Java Concurrency ----bit computing

Preface:

When we browse the source code of JDK, we find that it uses a lot of bit operation. Although increasing the difficulty of reading on the surface, but its higher operating efficiency, but also reduce the number of variables defined. Here we take a quick look at bit computing, and to elaborate a practical example of its practical application.

Bit computing:

  1. 位与 &
    1 & 1 =1
    1 & 0 =0
    0 & 0 = 0
  2. Bit or |
    . 1 | =. 1. 1
    . 1 | = 0. 1
    0 | 0 = 0
  3. THE NON ~
    ~ 1 = 0
    ~ 1 0 =
  4. Bitwise XOR ^
    1 ^ 1 = 0
    1 0 = 1 ^
    0 ^ 0 = 0
  5. << Signed left
    a signed right shift >>
    (rule: the sign bit unchanged, a positive number up 1 high, high up negative 0)
  6. Unsigned Right: >>>
    (: Higher fill all 0)
  7. Modulo operation:
    A% (^ n-2) is equivalent to a & (2 ^ n -1)

Simple code example:

package 位运算;


public class InToBinary {
    public static void main(String[] args) {

        int data = 4;
        System.out.println("the "+data+" is "+Integer.toBinaryString(data));

        //位与 & (1&1=1 1&0=0 0&0=0)
        System.out.println("the 4 is "+Integer.toBinaryString(4));
        System.out.println("the 6 is "+Integer.toBinaryString(6));
        System.out.println("the 4&6 is "+Integer.toBinaryString(4&6));
        //位或 | (1|1=1 1|0=1 0|0=0)
        System.out.println("the 4|6 is "+Integer.toBinaryString(4|6));
        //位非 ~ (~1=0  ~0=1)
        System.out.println("the ~4 is "+Integer.toBinaryString(~4));
        //位异或 ^ (1^1=0 1^0=1 0^0=0)
        System.out.println("the 4^6 is "+Integer.toBinaryString(4^6));

        //取摸的操作 a % (2^n) 等价于 a & (2^n-1)
        System.out.println("the 345 % 16 is "+(345%16)+
        " or "+(345&(16-1)));
    }
}

The results show:
Here Insert Picture Description

After the above example, we can only preliminary understanding of its rules of operation, but someone will ask, what does it apply in real life we ​​do?

In practice, the user's permissions will inevitably and ultimately, additions and deletions to change search data, may not know when the bit operation, we most often use is to use a few flags to judge, but when we increased authority, we will have to continue to define the flag, it would be our program code becomes quite cumbersome, but if you use a bit operation are identified, then our flag will be greatly reduced, simply use an int type bit (can represent 32 kind permission) to represent each bit represents one authority, so that we greatly simplify the amount of code, but also greatly optimize our permission judgment step. (Using binary bits in actual electricity supplier's application to represent the properties of different products is very common)

Simple code example: [We started to set all the permissions it has CRUD, and then use the Delete permission (to delete the delete and insert privileges), and then determine whether the relevant authority after the changes we have. ]

package 位运算;

/**
 * 权限的设置
 */
public class Permission {

    //是否允许查询,二进制第一位,0表示否,1表示是
    public static final int ALLOW_SELECT = 1 << 0; //0001 = 1
    //是否允许新增,二进制第二位,0表示否,1表示是
    public static final int ALLOW_INSERT = 1 << 1; //0010 = 2
    //是否允许修改,二进制第三位,0表示否,1表示是
    public static final int ALLOW_UPDATE = 1 << 2; //0100 = 4
    //是否允许删除,二进制第四位,0表示否,1表示是
    public static final int ALLOW_DELETE = 1 << 3; //1000 = 8

    //存储目前大的权限状态
    private int flag;

    //设置用户权限
    public void setPer(int per){
        flag = per;
    }
    //增加用户权限(1个或者多个)
    public void enable(int per){
        flag = flag|per;
    }
    //删除用户的权限(1个或者多个)
    public void disable(int per){
        flag = flag&~per;
    }
    //判断用户的权限
    public boolean isAllow(int per){
        return ((flag & per) == per);
    }
    //判断用户没有的权限
    public boolean isNotAllow(int per){
        return ((flag & per) ==0);
    }
   //我们一开始设置它拥有增删改查的所有权限,然后使用删除权限(删除了可删除和可插入的权限)
   //然后判断修改后我们相关权限是否拥有
    public static void main(String[] args) {
        int flag = 15;
        Permission permission = new Permission();
        permission.setPer(flag);
        permission.disable(ALLOW_DELETE|ALLOW_INSERT);
        System.out.println("select = "+permission.isAllow(ALLOW_SELECT));
        System.out.println("insert = "+permission.isAllow(ALLOW_INSERT));
        System.out.println("update = "+permission.isAllow(ALLOW_UPDATE));
        System.out.println("delete = "+permission.isAllow(ALLOW_DELETE));
    }
}

The results show
Here Insert Picture Description
Summary: bit computing not only visible in the JDK source code everywhere, in the later application of our design-related user rights and commercial properties are also very common, so long as we understand the relevant rules of operation, and then a simple example as auxiliary, design, we will be handy.

Published 19 original articles · won praise 2 · Views 415

Guess you like

Origin blog.csdn.net/TheWindOfSon/article/details/103963755