Remember to learn Java bit operations

Java-bit operator

  1. "Bitwise AND" operation
    "Bitwise AND" operator's operation sign bit "&" is a binocular operator, and its algorithm is: if the corresponding bits of two integer data a and b are both 1, the result bit It is 1, otherwise it is 0.
    The binary of the integer 5 is: 00000000 00000000 00000000 00000101
    The binary of the integer 4 is: 00000000 00000000 00000000 00000100
    5&4 The result is: 00000000 00000000 00000000 00000100, which is equal to 4 in decimal.

  2. "Bitwise OR" operation
    The operator bit "|" of the "Bitwise OR" operator is a binocular operation, and its algorithm is: if the corresponding bits of the two operands are both 0, the result is 0, otherwise It is 1.
    The binary of the integer 3 is: 00000000 00000000 00000000 00000011
    The binary of the integer 6 is: 00000000 00000000 00000000 00000110
    3|The result of 6 is: 00000000 00000000 00000000 00000111, the decimal representation 7

  3. "Bitwise negation" operation
    "Bitwise negation" operation is also called "bitwise negation" operation, the operator is "~". The rule is: change the 1 in the operand binary to 0, and 0 to 1.

  4. "Bitwise XOR"
    The operator bit "^" of the "Bitwise XOR" operation is a binocular operator. The rule is: when the binary representation of the two operands is the same (0 or 1 at the same time) , The result is 0, otherwise it is 1.

  5. Shift operation (applicable data types are byte, short, char, int, long)
    (1): << Left shift: shift the binary data of the left operand in the memory to the left by the number of bits specified by the right operand, to the left Fill in the empty part with 0.
    (2): >> Right shift: If the highest bit is 0, the left-shift empty bit is filled with 0; the highest bit is 1, the right-shifted empty bit is filled with 1.

Guess you like

Origin blog.csdn.net/weixin_39998155/article/details/90795974