一,setBit testBit权限管理的理解
假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击http://106.12.206.16:8080/qingruihappy/index.html
1.1、jdk7文档解释
public boolean testBit(int n)
Returns true if and only if the designated bit is set. (Computes((this & (1<<n)) != 0).)
Parameters:
n - index of bit to test.
Returns:
true if and only if the designated bit is set.
Throws:
ArithmeticException -n is negative.
翻译:
当且仅当指定的位被设置时返回true。
1.2,代码
1 public class TestBit { 2 3 public static void main(String[] args) { 4 BigInteger bi = new BigInteger("12"); 5 6 //testBit 的判断条件为((this & (1<<n)) != 0 7 System.err.println("Test Bit on " + bi + " at index 1 returns "+bi.testBit(1)); 8 System.err.println("Test Bit on " + bi + " at index 2 returns "+bi.testBit(2)); 9 System.err.println("Test Bit on " + bi + " at index 3 returns "+bi.testBit(3)); 10 System.err.println("Test Bit on " + bi + " at index 4 returns "+bi.testBit(4)); 11 //12 的二进制表示为1100 12 //1 的二进制表示为0001 ,1<<1 为00000010, (this & (1<<1))为0,bi.testBit(1)为FALSE 13 //2 的二进制表示为0010 ,1<<2 为00000100, (this & (1<<2))为4,bi.testBit(2)为true 14 //3 的二进制表示为0011 ,1<<3 为00001000, (this & (1<<3))为8,bi.testBit(3)为true 15 //4 的二进制表示为0100 ,1<<4 为00010000, (this & (1<<4))为0,bi.testBit(4)为FALSE 16 } 17 18 }
Test Bit on 12 at index 0 returns FALSE
因为1<<0 还是1。
1.3,java基础解释
& 既是位运算符又是逻辑运算符,&的两侧可以是int,也可以是boolean表达式,当&两侧是int时,要先把运算符两侧的数转化为二进制数再进行运算。
①12&5 的值是多少?答:12转成二进制数是1100(前四位省略了),5转成二进制数是0101,则运算后的结果为0100即4 这是两侧为数值时;
② 若 int i = 2,j = 4;则(++i=2)&(j++=4)的结果为false,其过程是这样的:先判断++i=2是否成立,这里当然是不成立了(3 == 2),但是程序还会继续判断下一个表达式是否成立,j++=4 ,该表达式是成立的,但是&运算符要求运算符两侧的值都为真,结果才为真,所以(++i=2)&(j++=4)的结果为 false
<< 移位运算符就是在二进制的基础上对数字进行平移。按照平移的方向和填充数字的规则分为三种:<<(左移)、>>(带符号右移)和>>>(无符号右移)。
比如
3 << 2 过程是0011 ----》 1100
上面代码中已经解释了
//12 的二进制表示为1100
//1 的二进制表示为0001 ,1<<1 为00000010, (this & (1<<1))为0,bi.testBit(1)为FALSE
//2 的二进制表示为0010 ,1<<2 为00000100, (this & (1<<2))为4,bi.testBit(2)为true
//3 的二进制表示为0011 ,1<<3 为00001000, (this & (1<<3))为8,bi.testBit(3)为true
//4 的二进制表示为0100 ,1<<4 为00010000, (this & (1<<4))为0,bi.testBit(4)为FALSE
这里说一下&,当1100&0010时,二进制每一位上的数与操作,都为1时得1,其他情况为0,所以1100&0010为0000
1.4,setBit 的原理
BigInteger bi = new BigInteger("12");
bi =bi.setBit(2);
bi =bi.setBit(4);
bi的值将是12+2^5 =28
jdk解释:
-
public BigInteger setBit(int n)
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set. -
(Computes
(this | (1<<n))
.)
12的二进制为00001100,
12|(1<<2)为1100|0100=1100,所以12|(1<<2)为12
12|(1<<4)为1100|0001000=00011100,12|(1<<4)为28
二,项目中的应用
2.1,testBit
1 public static BigInteger sumRights(String[] rights){ 2 BigInteger num = new BigInteger("0"); 3 for(int i=0; i<rights.length; i++){ 4 num = num.setBit(Integer.parseInt(rights[i])); 5 } 6 return num; 7 }
在这里会把
[1, 2, 36, 37, 39, 38, 43, 20, 3, 40, 41]
0+2的2次方+2的36次方+2的37次方。。。。。
13125421105166
算出来一个值赋给num,并存到数据库中
2.2,testBit
1 public static boolean testRights(BigInteger sum,int targetRights){ 2 return sum.testBit(targetRights); 3 }
在这里会把从数据库中取到的值sum13125421105166通过testBit这个方法,因为这个值是通过上面的方法来的,现在通过testBit这个方法假如在[1, 2, 36, 37, 39, 38, 43, 20, 3, 40, 41]这几个数中则返回true,否则则返回false。