• setBit testBit权限管理


    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。

     

     

     

    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
        }
    
    }

    结果

    Test Bit on 12 at index 0 returns   FALSE

    因为1<<0  还是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

    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

     

    这里说一下&,当1100&0010时,二进制每一位上的数与操作,都为1时得1,其他情况为0,所以1100&0010为0000

     

    5、

     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

    作者:逆舟
    https://www.cnblogs.com/zy-jiayou/
    本博客文章均为作者原创,转载请注明作者和原文链接。
  • 相关阅读:
    mysql常用命令
    Navicat连接MySQL数据库出现 ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded
    java——Spring(3)
    java———Spring(2)(补充)
    Java——Spring(1)
    Struts2框架(2)
    Struts2框架(1)
    java——Mybatis(2)
    java——MyBatis(1)
    java——Hibernate(2)
  • 原文地址:https://www.cnblogs.com/zy-jiayou/p/8274689.html
Copyright © 2020-2023  润新知