bitwise logical operators

bitwise logical operators

There are 4 bit logic operators: & (and), | (or), ~ (not) and ^ (exclusive or). Except for ~ (bitwise negation), which is a unary operator, the rest are binary operators. Their basic usage is listed in Table 1.

operator meaning example result
& Bitwise AND operation (AND) 4 & 5 4
| Bitwise OR operation (OR) 4 | 5 5
^ Bitwise Exclusive OR (XOR) 4 ^ 5 1
~ Bitwise negation operation (NOT) ~ 4 -5

bitwise AND operator

The bitwise AND operator is &, and its operation rules are: the numbers involved in the operation are aligned in the low order, and the high order is filled with zeros. If the corresponding binary bit is 1 at the same time, the calculation result is 1, otherwise it is 0. Therefore, the result of bitwise ANDing any number with 0 is 0.

For example the following expression:

100&0

Figure 1 shows the operation process, and the result is 0.

img
Figure 1 The operation process of 100 bits and 0

The following is the process of performing a bitwise AND operation on two non-zero numbers.

int x = 5,y = 12; // 创建整型变量保存两个数int z = x&y; // 对这两个数进行位与运算,结果保存到z

After these two lines of statements are executed, the value of variable Z is 4. Figure 2 shows the operation process.

img
Figure 2 The operation process of 5 bits and 12

bitwise OR operator

The bit OR operator is |, and its operation rules are: the numbers involved in the operation are aligned in the lower order, and the higher order is insufficient to fill with zeros. If only one of the corresponding binary bits is 1, then the result is 1; if the corresponding binary bits are all 0, the result is 0.

Below is an expression using the bitwise OR operator.

11|7

The operation result is 15, and Figure 3 shows its operation process.

img
Figure 3 The operation process of 11 or 7

Bitwise XOR operator

The bit-exclusive OR operator is ^, and its operation rules are: the numbers involved in the operation are aligned in the low order, and the high order is insufficient to fill zero. If the corresponding binary bits are the same (both 0 or 1 at the same time), the result is 0; if the corresponding If the bits are different, the result is 1.

Below is an expression using the bitwise XOR operator.

11^7

The operation result is 12, and Figure 4 shows its operation process.

img
Figure 4 Operation process of 11-bit XOR 7

Tip: In some high-level languages, the operator is used ^as an exponentiation operator, and attention should be paid to the distinction.

bit negation operator

The bit inversion operator is ~, and its operation rule is: only one operand is operated, and the 1 in the operand binary is changed to 0, and the 0 is changed to 1.

Below is an expression using the bitwise negation operator.

~10

The operation result is 65525, and Figure 5 shows its operation process.

img
Figure 5 The operation process of inverting 10 bits

We can use the following program to check the result of this operation.

int i = 10;System.out.printf("%d \n",~i);

Compile and execute the above program, and you will find that the output is -11 instead of 65525. This is because the result after negation is a hexadecimal number, and %d is used in the above program to convert the output to a decimal number.

You can use the following statement to view the hexadecimal results.

int i=10;System.out.printf("%x \n",~i);

You can see that the output result is fff5, which is 1111111111110101 when converted to binary. The highest bit of this binary number is 1, indicating that the number is negative. In addition to the highest bit, add 1 to the bitwise inversion to get the original binary code 1000000000001011, which is -11 in decimal.

Note: The operands of bit operators can only be integer or character data and their variants, not for complex data types such as float, double or long.

shift operator

Shift operators are used to move an operand in a certain direction (left or right) by a specified number of binary digits. Table 2 lists two displacement operators in the Java language, both of which are binocular operators.

Shifting one bit to the right is equivalent to dividing by 2, shifting n bits is equivalent to dividing by 2 n 2^n2n power

operator meaning example result
» right shift operator 8»1 4
« left shift operator 9«2 36

left shift operator

The left shift operator is «, and its operation rule is: shift all the numbers to the left by the corresponding number of digits in binary form, remove the high bit (discard), and fill the vacancy in the low bit with zero.

For example, the process of shifting the integer 11 to the left by 1 bit is shown in Figure 6.

img
Figure 6 Operation process of shifting 11 to the left by 1 bit

As can be seen from Figure 6, all binary bits of the original number are shifted to the left by 1 bit. The highest bit 0 originally located on the left is shifted out and discarded, and then 0 is added to the end to make up the bit. The final result is 22, which is equivalent to twice the original number.

right shift operator

The right shift operator is », and its operation rule is: move all the numbers to the right by the corresponding number of digits in binary form, remove the low bits (discard), and fill the high bits with zeros.

For example, the process of shifting the integer 11 to the right by 1 bit is shown in Figure 7.

img
Figure 7 Operation process of shifting 11 to the right by 1 bit

As can be seen from Figure 7, all the binary bits of the original number are shifted to the right by 1 bit. The lowest bit 1 originally located on the right is shifted out and discarded, and then 0 is added to the highest bit to make up. The final result is 5, which is equivalent to the result of dividing the original number by 2.

compound bitwise assignment operator

All binary bitwise operators have a shorthand form that combines assignment with bitwise operations. Compound bitwise assignment operators consist of assignment operators combined with bitwise logical and shift operators. Table 3 lists the combined compound bit assignment operators.

operator meaning example result
&= bitwise and assignment num1 &= num2 Equivalent to num 1=num 1 & num2
|= bitwise or assignment num1 |= num2 Equivalent to num 1=num 1 | num2
^= bitwise XOR assignment num1 ^= num2 Equivalent to num 1=num 1 ^ num2
-= bitwise inverse assignment num1 -= num2 Equivalent to num 1=num 1 - num2
«= bitwise left shift assignment num1 «= num2 Equivalent to num 1=num 1 « num2
»= bitwise right shift assignment num1 »= num2 Equivalent to num 1 = num 1 » num2

The following program defines several int variables, and then assigns the calculated value to the corresponding variable by using the abbreviated form of bit assignment:

int a = 1;int b = 2;int c = 3;a &= 4;a |= 4;a ^= c;a -= 6;b >>= 1;c <<= 1;System.out.println("a = " + a);System.out.println("b = " + b);System.out.println("c = " + c);

The output of this program is:

a = 1
b = 1
c = 6

Guess you like

Origin blog.csdn.net/qq_41704415/article/details/126401013