bit operation

1. Bitwise AND Operator (&)

The two objects participating in the operation are "anded" according to the binary bit
 
Operation rules:
0 & 0 = 0;0 & 1 = 0;1 & 0 = 0;1 & 1 = 1;
E.g:
3 & 5
0011 & 0101 = 0001
So 3 & 5 = 1
 

2. Bitwise OR operator (|)

The two objects participating in the operation are "ored" according to the binary bit
 
Operation rules:
0 | 0 = 0;0 | 1 = 1;1 | 0 = 1;1 | 1 = 1;
E.g:
3 | 5
0011 & 0101 = 0111
So 3 | 5 = 7
 

3. Bitwise XOR operator (^)

The two objects participating in the operation are "exclusively ORed" according to the binary bits
 
Operation rules:
0 ^ 0 = 0;0 ^ 1 = 1;1 ^ 0 = 1;1 ^ 1 = 0;
E.g:
3 ^ 5
0011 ^ 0101 = 0110
So 3^5 = 6

4. Negation operator (~)

The two objects participating in the operation are "inverted" according to the binary bit
 
Operation rules:
~1 = 0;  ~0 = 1;
That is: bitwise inversion of a binary number, that is, 0 becomes 1, 1 becomes 0
E.g:
~9
That is, 1001 becomes 0110
 

5. Left shift operator (<<)

Shifts all the binary bits of an operator object to the left by several bits (the left binary bits are discarded, and the right side is filled with 0).

For example: a = a << 2 Shift the binary bits of a to the left by 2 bits, and add 0 to the right,
a = a * 2 after shifting left by 1;
If the high bit discarded when shifting to the left does not contain 1, each left shift is considered to be equivalent to multiplying the number by 2
 

6. Right shift operator (>>)

Shift all the binary bits of a number to the right by a few bits, add 0 to the left for positive numbers, add 1 to the left for negative numbers, and discard the right.
Each right shift of the operand is equivalent to dividing the number by 2.
For example: a = a >> 2 shifts the binary bits of a to the right by 2,
Left-complement with 0 or with 1, that is, whether the shifted number is positive or negative.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324649067&siteId=291194637