Detailed explanation of bit operations in Java

When doing problems recently, I often encounter bit operations. Here is a summary of bit operations i in java for review.

 

1. What is bit operation? What are the benefits of using bit operations?

 

1. What is bit operation?

    The computer uses two's complement at the bottom to perform operations. The corresponding binary bits are operated, and the computer only recognizes 0 and 1.

2. The benefits of bit operation:

    Clever use of bit operations can greatly reduce operating costs and optimize algorithms.

Second, the 7 bit operators supported by Java

&: And operator

|: OR operator

~: Not operator

^: XOR operator

>>: Right shift operator

<<: Left shift operator

>>>: unsigned right shift operator

Third, the actual use of bitwise operators in Java

Displacement operation: (only valid for the int type. In Java, the length of an int is always 32 bits, which is 4 bytes. It operates on the binary number of the integer). It can also be applied to the following types, namely byte, short, char, long (they are all in integer form). When these four types are used, the JVM first converts them to int types before performing operations.

1. And (&) operator, only 1 when both are 1, and 0 in other cases

Example: -5 & 4

   The binary form of -5 is: 11111111 11111111 11111111 11111011

    The binary form of 4 is: 00000000 00000000 00000000 00000100

    After logical operation, it is: 00000000 00000000 00000000 00000000

    Converted to decimal: 0

2. Or (|) operator, it is 0 when both are 0, and it is 1 in other cases

Example: -5 | 4

    The binary form of -5 is: 11111111 11111111 11111111 11111011

    The binary form of 4 is: 00000000 00000000 00000000 00000100

    After logical operation is: 11111111 11111111 11111111 11111111

    Converted to decimal: -1

3. Not (~) operator, inverted, that is, 1 becomes 0, and 0 becomes 1

Example: ~ (-5)

    The binary form of -5 is: 11111111 11111111 11111111 11111011

    The inverted binary form is: 00000000 00000000 00000000 00000100

    Converted to decimal: 4

4. Exclusive OR (^) operator, the same value is 0, the different value is 1

Example: -5 ^ 4

    The binary form of -5 is: 11111111 11111111 11111111 11111011

    The binary form of 4 is: 00000000 00000000 00000000 00000100

    After logical operation is: 11111111 11111111 11111111 11111111

    Converted to decimal: -1

5. Right shift (>>) operator, m>>n, shifts the binary number of m to the right by n places, m is a positive number, all high bits are filled with 0, m is a negative number, high bits are filled with 1

Note: When the number does not overflow: m>>n is equivalent to m divided by 2 to the power of n. When the number obtained is an integer, it is the result. When the number obtained is negative, there are two values ​​according to m Happening

1. When m is a positive number, the obtained chamber of commerce discards decimal places

2. When m is a negative number, the obtained chamber will discard the decimal place, and then add the integer part +1 to get the result

Example 1: 5>>2


    The binary form of 5 is: 00000000 00000000 00000000 00000101

    After the logic operation is performed (shift two bits to the right): 00000000 00000000 00000000 00000001

    Converted to decimal: 1

Example 2: 5>>4


    The binary form of 5 is: 00000000 00000000 00000000 00000101

    After logic operation, it is (right shift by four bits): 00000000 00000000 00000000 00000000

    Converted to decimal: 0

Note: The ones after the decimal point are discarded here

Example 3: -5>>2

     The binary form of -5 is: 11111111 11111111 11111111 11111011

    After logical operation, it is (shift two bits to the right): 11111111 11111111 11111111 11111110

    Converted to decimal: -2

6. Left shift (<<) operator, m<<n, shifts the binary number of m to the left by n places, discards all the high digits exceeding n digits, and fills in the low digits with 0 (in this case, a positive number may become a negative number)

Note: When the number does not overflow, for integers and negative numbers, m<<n is equivalent to m multiplied by 2 to the power of n.


例1:5<<2

    The binary form of 5 is: 00000000 00000000 00000000 00000101

    After the logic operation is performed (shift two bits to the left): 00000000 00000000 00000000 00010100

    Converted to decimal: 20

例2:5<<29


    The binary form of 5 is: 00000000 00000000 00000000 00000101

    After the logic operation is performed (shift two bits to the left): 10100000 00000000 00000000 00000000

    Converted to decimal: -1610612736

7. Unsigned right shift (>>>) operator, m>>>n, the binary right shift represented by the integer m is n bits, no matter the positive or negative number, the high bits are filled with 0

例1: 5>>>2

    The binary form of 5 is: 00000000 00000000 00000000 00000101

    After the logic operation is performed (unsigned right shift two bits): 00000000 00000000 00000000 00000001

    Converted to decimal: 1

例2:-5>>>2

    The binary form of -5 is: 11111111 11111111 11111111 11111011

    Perform logic operation guard (shift two bits to the right without sign): 00111111 11111111 11111111 11111110

    Converted to decimal: 1073741822

Four, summary

The reason for the fast bit operation is to directly follow the computer's low-level binary machine operation instructions, and our program code operation is finally converted by the JVM into a computer executable binary operation instruction. The bit operation omits the intermediate conversion operation, and the processor operates directly. Faster
 

Guess you like

Origin blog.csdn.net/m0_46405589/article/details/115309902