Understanding of bit operations-AND, OR, XOR, negation, shift left, shift right

What is bit operation

The numbers in the program are all binary storage methods used in the computer, and the bit operation is to directly manipulate the binary bits of the integer in the memory.

calculating signs

symbol meaning Algorithm
& Bitwise and When both bits are 1, the result is 1, which can be used to determine parity
| Bitwise or When both bits are 0, the result is 0
^ Bitwise XOR When two bits are the same, it is 0, when they are different, it is 1, which can be used to count different numbers
~ Bitwise negation 0 becomes 1, 1 becomes 0
<< Shift left All binary bits are shifted to the left by several bits, the high bits are discarded, and the low bits are filled with 0
>> Signed shift right All binary bits are shifted to the right by several bits. For unsigned numbers, high bits with 0, and signed numbers, the processing methods of each compiler are different, some complement the sign bit (arithmetic shift right), and some complement 0 (logical shift right)
>>> Unsigned right shift All binary bits are shifted to the right by several bits. For unsigned numbers, high bits with 0, and signed numbers, the processing methods of each compiler are different, some complement the sign bit (arithmetic shift right), and some complement 0 (logical shift right)

Operation description

Bit logic operation result

a b a&b(a and b) a|b(a or b) a^b(a xor b) ~a(not a)
0 0 0 0 0 1
1 0 0 1 1 0
0 1 0 1 1 1
1 1 1 1 0 0

AND operation-&

When the same bits of two binary numbers are 1 , return 1; otherwise, return 0.

And operation is usually used for binary bit fetch operation. For example , the result of a number &1 is to take the last digit in binary, because the binary form of decimal number 1 is 0000 0000 0000 0001 , and 0&1=0, 0&0=0 , so the result of any number &1 divided by the last digit is 0 , when the last bit of the number x is 0, x&1 gets 0000 0000 0000 0000 (ie 0) , then this number is an even number, when the last bit of the number x is 1, x&1 gets 0000 0000 0000 0001 (ie 1) , Then this number is odd.
Insert picture description here

OR operation—— |

When two binary numbers have the same bit as long as there is one 1 , it returns 1, otherwise it returns 0.

Or operation is usually used for unconditional assignment of specific bits in binary. For example, the result of a number or 1 is to force the last bit of the binary to be 1 , because the last bit of the binary of 1 is 1, and 1 | 0 = 1, 1 | 1 = 1 . If you need to change the last bit of the binary to 0, just subtract one after this number or 1. The actual meaning is to force this number into the closest even number.
Insert picture description here

XOR operation-^

When the same bits of two binary numbers are both 1 or 0 (that is, the same bits are the same), it returns 0, otherwise it returns 1.

The inverse operation of the XOR operation is itself, which means that the final result of XOR the same number twice remains unchanged, that is, (a ^ b) ^b = a .

A number of exclusive OR itself result is 0, i.e. A = A ^ 0 .Insert picture description here

Inversion operation-~

The inversion operation is to invert all 0 and 1 in the binary number, that is, ~1=0, ~0=1 .
Insert picture description here

Left shift operation-<<

a << b means that after converting a to binary, shift b bits to the left, discard the overflow bit, and add b zeros at the end .

a << b is actually a multiplied by 2 to the power of b, because adding a 0 at the end of a binary number is equivalent to multiplying the number by 2, that is, 10<<3 = 10*2 3 = 80, 1<<31 = 2 31 .

It is generally considered that a <<1 is faster than a*2 , because the former is a lower-level operation. It is recommended that multiplying by 2 in the program should be replaced by a left shift as much as possible.
Insert picture description here
Insert picture description here

Right shift operation—— >>

a >> b means that after converting a to binary, shift b bits to the right, and discard the overflow bit. However, there are two cases: “signed right shift” and “unsigned right shift”.

a >> b is actually a divided by 2 to the power of b, that is, 48>>1 = 24, -80>>2 = -20 .
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44115164/article/details/109274881