Bitwise operators include: & (bitwise AND), | (bitwise OR), ^ (bitwise OR), and ),~ (Bitwise inversion ).
The priority ranges from high to low ~ , &, ^, |
Bitwise AND operation have two typical usage: one is to take a certain bit of the information of a single bit string, as shown belowCodeMinimum 7 digits of X: X & 0177. Second, let a variable retain a certain number of digits, and the remaining position is 0. For example, the following code keeps X at least 6 digits: x = x & 077.
A typical usage of bitwise OR operations is to set the location of a single bit string to 1. If you want to obtain the rightmost 4 bits as 1, the other bits must be the same as the other bits of the variable J. You can use the logic or operation 017 | J;
A typical usage of bitwise exclusive or operation is to reverse the information of a single bit string. For example, if you want to reverse the information of the rightmost 4 bits of the integer variable J, you can use the logic XOR operation 017 ^ J to reverse the information of the rightmost 4 bits of J, that is, the first bits, the result is 0, and the original value is 0. The result is 1. Exchange two values without temporary variables, for example, a = 3, B = 4. To swap values of A and B, use the following value assignment statement:
A = a ^ B; B = B ^ A; A = a ^ B;
Inverse operations are often used to generate constants irrelevant to system implementations. To set the minimum position of variable X to 0, the remaining digits remain unchanged, use the code X = x &~ 077.