[Notes] Bitwise operators-dry goods!

Bitwise operator

<< >> ~ | ^ &

(<<) Left shift operation: equivalent to multiplying by 2 to the power of n

(>>) Right shift operation: equivalent to dividing by 2 to the power of n

(~) Negation operation: bitwise inversion operator, 0 is 1

(|) OR operation: bitwise OR operator, one is 1 then 1

(^) XOR operation: bitwise XOR operator, the difference is 1

(&) AND operation: bitwise AND operator, all 1 is 1

priority:

2 3 4 5 8 9 10
~ * / % + - << >> & ^ |

 

Common binary bit conversion operations

purpose Paraphrase Means
Remove the last digit 101101->10110 x>>1
Add a 0 at the end 101101->1011010 x<<1
Change the last digit to 1 101100->101101 x | 1
Change the last digit to 0 101101->101100 (x |1) - 1
The last bit is negated 101101->101100 x ^ 1
Turn the Kth from the right to 1 101001->101101,k=3 x | (1<<(k-1))
Turn the Kth bit from the right to 0 101101->101101,k=3 x & ~(1<<(k-1))
The kth from the right is reversed 101001->101101,k=3 x ^ (1<<(k-1))
Take the last three 1101101->101 x &7

 

The & operation is usually used for binary bit operation. For example, the result of a number &1 is to take the last bit of the binary.

This can be used to determine the parity of an integer. The last bit of the binary is 0 to indicate that the number is even, and the last bit is 1 to indicate that the number is odd

| Operations are usually used for unconditional assignments on specific bits of the binary. For example, the result of a number |1 is to force the last bit of the binary to 1

If you need to change the last bit of the binary to 0, just subtract one after this number|1. The actual meaning is to force this number into the nearest even number.

The ^ operation is usually used to invert a specific bit of the binary. The inverse operation of the ^ operation is itself, which means that the final result of the two exclusive or the same number remains unchanged, that is (a^b)^b=a ;

^ Operation can be used for simple encryption, such as the original value int a = 19880516; key int key =1314520; for encryption int data=key^a = 20665500; decryption data^key == a;

^Operation can also achieve the exchange of two values ​​without intermediate variables 

<<Operation

a<<b means to shift a to the left by b bits after converting a to binary (add b 0s at the end). For example, the binary representation of 100 is 1100100, and 100 is shifted by 2 digits to the left (add 2 zeros afterwards): 1100100<<2 =110010000 =400, it can be seen that the value of a<<b is actually a multiplied by 2 b Power, because adding a 0 after a binary number is equivalent to multiplying the number by 2, and 2 zeros, or 2 to the second power, equals 4. It is generally considered that a<<1 is faster than a*2 because the former is a lower level operation. Therefore, the operation of multiplying by 2 in the program is replaced by shifting one bit to the left as much as possible.

Defining some constants may use the << operation. You can easily use 1<<16 -1 to represent 65535 (unsingned int maximum 16-bit system). Many algorithms and data structures require that the data module must be a power of 2. In this case, you can use << to define MAX_N and other constants.

>>Calculation

Similar to <<, a>>b represents a binary right shift by b bits (removing the last b bits), which is equivalent to dividing a by 2 to the b power (rounding). We often use >>1 instead of /2 (div 2), such as binary search, heap insertion operations, and so on. Finding a way to replace the division operation with >> can greatly improve the efficiency of the program. The binary algorithm of the greatest common divisor uses a division by 2 operation to replace the slow and surprisingly% (mod) operation, and the efficiency can be increased by 60%.

 

 

example

A = 0011 1100

B = 0000 1101

Operator description Instance
& Bitwise AND operation, and binary bit for "and" operation. Operation rules: 0&0=0; 0&1=0; 1&0=0; 1&1=1; (A & B) will get 12, which is 0000 1100
| The bitwise OR operator, performs "or" operation by binary bits. Operation rules: 0|0=0; 0|1=1; 1|0=1; 1|1=1; (A | B) will get 61, which is 0011 1101
^ XOR operator, performs "XOR" operation based on binary bits. Operation rules: 0^0=0; 0^1=1; 1^0=1; 1^1=0; (A ^ B) will get 49, which is 0011 0001
~ The negation operator performs "negation" operation based on binary bits. Operation rules: ~1=0; ~0=1; (~A) will get -61, which is 1100 0011, the complement of a signed binary number.
<< Binary shift left operator. Shift all binary digits of an operand to the left by several bits (the binary digits on the left are discarded, and the right is filled with 0). A << 2 will get 240, which is 1111 0000
>> Binary shift right operator. Shift all the binary digits of a number to the right by several bits, add 0 to the left for positive numbers, add 1 to the left for negative numbers, and discard on the right. A >> 2 will get 15, which is 0000 1111

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/113754598