天天看点

BigInteger权限判断

1、上篇文章已经解析了BigInteger.testBit(int n)与setBit(int n)

http://blog.csdn.net/u014520797/article/details/64122211

2、在进行开发权限系统时可以使用BigInteger.setBit(int n)与testBit(int n)进行权限设置和权限判断

3、代码

public static void main(String[] args) {
		BigInteger bi = new BigInteger("0"); 
		bi =bi.setBit(1);
		bi =bi.setBit(2);
		bi =bi.setBit(3);
		bi =bi.setBit(6);
		
		Boolean b1, b2,b3,b5;
		b1 = bi.testBit(1);
		b2 = bi.testBit(2);
		b3 = bi.testBit(3);
		b5 = bi.testBit(7);
		
		String str1 = "Test Bit on " + bi + " at index 1 returns " +b1;
		String str2 = "Test Bit on " + bi + " at index 2 returns " +b2;
		String str3 = "Test Bit on " + bi + " at index 3 returns " +b3;
		String str5 = "Test Bit on " + bi + " at index 7 returns " +b5;
		
		System.out.println( str1 );
		System.out.println( str2 );
		System.out.println( str3 );
		System.out.println( str5 );
	}
           

结果

Test Bit on 78 at index 1 returns true
Test Bit on 78 at index 2 returns true
Test Bit on 78 at index 3 returns true
Test Bit on 78 at index 7 returns false
           

4、解释

//在权限系统中,当一个权限资源的id为123456789.....
//当A拥有23资源的权限,那么就可以bi =bi.setBit(2或3或其他权限id),然后再去用bi.testBit(2或3或其他权限id)去验证,
           
//当拥有该权限时,便会返回true
//当B拥有456资源的权限,...........
//当C拥有789资源的权限,...........
			
//当然  也可不需要一个的去设置bi =bi.setBit(int n),比如A拥有2,3权限时,那么他的权限值为12(即1100),
//所以可以用new BigInteger("12").setBit(2或3或其他权限id)
           

5、如果有不懂,可以看上一篇的方法解释