**Document directory**
- Bitwise OR operator (|)
- XOR operator (^)
- Inverse Operator (~)
- Left shift operator (<)
- Shift right operator (>)
- Compound assignment operator

The two data involved in the operation perform the "and" Operation in binary bits.

Calculation rule: 0 & 0 = 0; 0 & 1 = 0; 1 & 0 = 0; 1 & 1 = 1;

That is, if the two digits are "1" at the same time, the result is "1". Otherwise, the value is 0.

For example, 3 & 5 means 0000 0011 & 0000 0101 = 0000 0001. Therefore, 3 & 5 is worth 1.

In addition, negative numbers are involved in bitwise AND computation in the form of supplementary codes.

Special uses of "and operations:

(1) Clear. If you want to clear a unit, even if all its binary bits are 0, the result is zero as long as it is a numerical phase that is zero to everyone.

(2) locate with one count finger

Method: Find a number, corresponding to the bit to be taken by X. The corresponding bit of this number is 1, and the remaining bit is zero. This number and X are "and operated" to obtain the position in X.

For example, set X = 10101110,

Take the 4 lower bits of X, and use X & 0000 1111 = 0000 1110;

It can also be used to obtain 2, 4, and 6 digits of X.

Bitwise OR operator (|)

The two objects involved in the operation perform the "or" Operation in binary bits.

Calculation rules: 0 | 0 = 0; 0 | 1 = 1; 1 | 0 = 1; 1 | 1 = 1;

That is, if one of the two objects involved in the operation is 1, the value is 1.

For example, 3 | 5 is 0000 0011 | 0000 0101 = 0000 0111. Therefore, 3 | 5 is worth 7.

In addition, negative numbers are involved in bitwise OR operations in the form of supplementary codes.

Special Functions of "or operation:

(1) It is often used for some locations of a data.

Method: Find a number, corresponding to the bit where X is set to 1, the corresponding bit of this number is 1, and the remaining bit is zero. This number is in the phase of X or enables some position 1 in X.

For example, you can use X | 10100000 0000 1111 = 1010 1111 to get the value at 4-Position 1.

XOR operator (^)

The two data involved in the operation perform the "XOR" operation based on the binary bit.

Calculation rules: 0 ^ 0 = 0; 0 ^ 1 = 1; 1 ^ 0 = 1; 1 ^ 1 = 0;

That is, the two objects involved in the operation. If the two corresponding bits are "different" (with different values), the result of this bits is 1; otherwise, it is 0.

Special Functions of "exclusive or operation:

(1) Let the specific positioning flip find a number, corresponding to everyone who wants to flip X, the corresponding bit of this number is 1, the remaining bit is zero, this number is different from the corresponding bit of X or.

For example, X = 10101110, so that X is flipped 4 bits low, which can be obtained by X ^ 0000 1111 = 1010 0001.

(2) It is different from 0 or retains the original value, x ^ 0000 0000 = 1010 1110.

You can see this point clearly in the above example.

Inverse Operator (~)

The bitwise operation is performed on a piece of data involved in the operation.

Calculation rules :~ 1 = 0 ;~ 0 = 1;

That is, the bitwise inversion of a binary number changes from 0 to 1 and from 0.

To make the bitwise of a number zero, it can be expressed as: &~ 1.

~ The value of 1 is 1111111111111110, and then the "and" operation is performed. The bitwise must be 0. Because "~" The priority of operators is higher than that of Arithmetic Operators, Relational operators, logical operators, and other operators.

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, A = A <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = A * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.

Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, A = A> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

> Operator*Expression1*To the right*Expression2*The specified number of digits.*Expression1*Is used to fill the left blank after right shift. The position removed from the right is discarded.

For example, after the following code is evaluated,*Temp*The value is-4:

-14 (that is, 11110010 of the binary) shifts the right two to 4 (that is, 11111100 of the binary ).

Vartemp =-14> 2

**Unsigned right shift operator (>>>)**

**>>> Operation**Operator*Expression1*To the right*Expression2*The specified number of digits. After the right shift, the left blank space is filled with zero. The bit removed from the right is discarded.

Example: vartemp =-14 >>> 2

Variable*Temp*Value**Is**-14 (that is, the binary 11111111 11111111 11111111 11110010), shifts two places to the right and then equals to 1073741820 (that is, the binary 00111111 11111111 11111111 ).

Compound assignment operator

**Bitwise operation**Combine with the value assignment operator to form a new compound value assignment operator, which is:

& = Example: A & = B is equivalent to a = A & B

| = Example: A | = B is equivalent to a = A | B

>>= Example: A >>> = B is equivalent to a = A> B

<= Example: a <= B is equivalent to a = a <B

^ = Example: a ^ = B is equivalent to a = a ^ B

Calculation rules: similar to the preceding calculation rules for compound assignment operators.

Data of different lengths

**Bitwise operation**
If two data entries of different lengths are used**Bitwise operation**The system will align the two to the right and then**Bitwise operation**.

Take the "and" operation as an example: we know that in C, the long type occupies 4 bytes, And the int type occupies 2 bytes, if a long data and an int data are "and", after the right side is aligned, the missing bits on the left side are supplemented according to the following three conditions,

(1) If the integer data is positive, add 16 zeros on the left.

(2) If the integer data is negative, add 16 1 on the left.

(3) If the integer data is unsigned, 16 zeros are added on the left.

For example: long a = 123; int B = 1; Calculate a & B.

For example: long a = 123; int B =-1; Calculate a & B.

For example: long a = 123; unsigned int B = 1; Calculate a & B.