Java bit mask control authority and introduction of of or of non of ~ greater than

  • 2021-07-06 10:51:14
  • OfStack

1. java bit mask

java bit mask is rarely used in java development, but when the system needs to judge whether an object has certain permissions, it can be done through bit mask.

Bit masks are mainly operated by bit operations, such as with ( & ), non-(~), or (), exclusive or (^), shift ( < < And > > ) and so on to realize the authority judgment function.

1.1 Brief introduction of 1 lower operator (all calculations are binary)

< < : Left shift operator, num < < 1, equivalent to num times 2 > > Right Shift Operator, num > > 1, equivalent to num divided by 2 > > > : Unsigned right shift, ignoring sign bits, and filling vacancies with 0 Exclusive OR (^): In 1 sentence, if the dissimilarity is true, boolean will be returned And ( & ): True if only two digits are 1, 0001 & 0101 is 0001 Or (): As long as one of the two digits is true, it is true, 0001 0100, which is 0101 Non-(~): to reverse itself.

Note that non-operation is special. Refer to the explanation of the great god on the Internet (if you can't understand it, turn over the principle of computer composition). In Java, all data representations are expressed in the form of complements. If there is no special explanation, the data type in Java defaults to int, and the length of int data type is 8 bits, and 1 bit is 4 bytes, that is, 32 bytes and 32bit.
For example: ~ 37
37 to binary is 100101

After the complement: 00000000 00000000 00000000 00100101
The reverse is: 11111111 11111111 111111 11011010

Because the high bit is 1, the original code is negative, and the complement of the negative number is the original code of its absolute value, and the end is added with 1.
Therefore, we can restore the complement of this binary number: first, subtract 1 at the end to get the inverse code: 11111111 11111111 11011001; Second, invert each bit to get the original code:
00000000 00000000 00000000 00100110, in which case the binary to the original code is 38
So ~ 37 =-38.

2. Bit mask control permissions

Suppose that in a system, users have four permissions: query (Select), add (Insert), modify (Update) and delete (Selete), and use masks to control and judge these permissions.

Code implementation:


package com.us.basics;
/**
 * Created by yangyibo on 17/12/11.
 *  To use a bit mask, you only need to use the 1 Is greater than or equal to 0 And less than 16 To represent all the integers of 16 The status of the permissions. 
 */
public class BitMask {
  public static int ADD = 1 << 0; //1*2 Adj. 0 Power   Adj. 2 Binary system  0001
  public static int DELETE = 1 << 1; //1*2 Adj. 1 Power   Adj. 2 Binary system  0010
  public static int UPDATE = 1 << 2; //1*2 Adj. 2 Power   Adj. 2 Binary system  0100
  public static int SELECT = 1 << 3; //1*2 Adj. 3 Power   Adj. 2 Binary system  1000
  //  Current status 
  private int currentStatus;
  BitMask(int currentStatus) {
    this.currentStatus = currentStatus;
  }
  /**
   *  Add an action permission   Achieve by or by operation 
   * @param more
   * @return
   */
  private BitMask append(int more) {
    currentStatus = currentStatus | more;
    return this;
  }
  /**
   *  Remove an operation permission   Through non-operation   And   Implement together with operation 
   * @param more
   * @return
   */
  private BitMask delete(int more) {
    //  If the non-operation is difficult to understand, it can be understood as   The subtraction (-) operation is also possible 
    // currentStatus = currentStatus - more;
    currentStatus &= ~more;
    return this;
  }
  /**
   *  Do you have a permission   Judge by and operation 
   * @param more
   * @return
   */
  private boolean isPermission(int more) {
    return (currentStatus & more) > 0 ;
  }
  public static void main(String[] args) {
    BitMask bk = new BitMask(BitMask.DELETE);
    // Add Permissions 
    bk.append(BitMask.ADD).append(BitMask.UPDATE);
    bk.delete(BitMask.ADD);
    //  Determine if there is  ADD  Operation authority 
    System.out.println(bk.isPermission(BitMask.ADD));
    test1();
  }
  /**
   *  Test and 
   */
  public static void test1() {
    int a =5; //0101
    int b =6; //0110
    System.out.println(a&b); // Output is  0100  For  4
  }
  /**
   *  Test or 
   */
  public static void test2() {
    int a =5; //0101
    int b =6; //0110
    System.out.println(a|b); //  Output is  0111  For  7
  }
  /**
   *  Test non-   It can be understood as from  a  Subtract from  b
   * ~5  Take  5 The complement of a positive number  0101  Is the absolute value, and the rest bits are filled with zeros. Then reverse it  1010  The highest position is  1  Is a negative number, and then takes its complement, and the complement of a negative number is the inverse of its absolute value  0101  , and then at the end +1 0110  So it's- 6
   *  Non-operational comparison around, can be understood as absolute value + 1  And take negative numbers 
   */
  public static void test3() {
    int a =5; //0101
    System.out.println(~a); //  Output is   - 6
  }
}

Source code for this article:

https://github.com/527515025/JavaTest/blob/master/src/main/java/com/us/basics/BitMask.java

Summarize


Related articles: