setBit testBit权限管理(shiro项目中来的二)

时间:2023-03-09 15:03:00
setBit testBit权限管理(shiro项目中来的二)

一,setBit testBit权限管理的理解

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,代码

 public class TestBit {

     public static void main(String[] args) {
         BigInteger bi = new BigInteger("12"); 

         //testBit 的判断条件为((this & (1<<n)) != 0
         System.err.println("Test Bit on " + bi + " at index 1 returns  "+bi.testBit(1));
         System.err.println("Test Bit on " + bi + " at index 2 returns  "+bi.testBit(2));
         System.err.println("Test Bit on " + bi + " at index 3 returns  "+bi.testBit(3));
         System.err.println("Test Bit on " + bi + " at index 4 returns  "+bi.testBit(4));
         //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
     }

 }

setBit testBit权限管理(shiro项目中来的二)

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

     public static BigInteger sumRights(String[] rights){
         BigInteger num = new BigInteger("0");
         for(int i=0; i<rights.length; i++){
             num = num.setBit(Integer.parseInt(rights[i]));
         }
         return num;
     }

在这里会把

[1, 2, 36, 37, 39, 38, 43, 20, 3, 40, 41]

0+2的2次方+2的36次方+2的37次方。。。。。

13125421105166

算出来一个值赋给num,并存到数据库中

2.2,testBit

     public static boolean testRights(BigInteger sum,int targetRights){
         return sum.testBit(targetRights);
     }

在这里会把从数据库中取到的值sum13125421105166通过testBit这个方法,因为这个值是通过上面的方法来的,现在通过testBit这个方法假如在[1, 2, 36, 37, 39, 38, 43, 20, 3, 40, 41]这几个数中则返回true,否则则返回false。