Deep understanding of Java bitwise operators

There is such a class of operators in Java that operate on binary. They are each &, |, ^, ~, >>, <<, >>> bitwise operators. No matter what base the initial value is based on, it will be converted into binary digits.

operate. The following is a brief explanation of these operators.

1.&:

The function is to perform the 'and' operation on the operators expressed in binary on both sides of the operator. And this operation is based on the same number of bits (bit) as the unit. The rules for the operation are: only if both operands are 1. The output is only 1. no

Then it is 0, and the ratio is as follows:

   12 的二进制为 1100

5 in binary is 0101

Then the binary of 12 & 5 is 0100, then the complete formula is 12 & 5 = 4;

2.|:

The function is to perform a bitwise 'or' operation on the operators expressed in binary on both sides of the operator. And this operation is based on the same number of bits (bit) as the unit.

The rule of operation is: only if both operands are 0, the output result is 0.

Otherwise, it is 1, and the scale is as follows:

12 in binary is 1100

5 in binary is 0101

Then 12 | 5 is 1101 in binary. Then the complete formula is 12 & 5 = 13;

3.^:

The function is to perform an 'exclusive OR' operation on the operands expressed in binary on both sides of the operator, and this operation is based on the same bit (bit) in the number.

The rules for the XOR operation are: only when the two operands are different. corresponding

The output result is 1, otherwise it is 0. The ratio is as follows:

12 in binary is 1100

5 in binary is 0101

Then 12 | 5 is 1001 in binary. Then the complete formula is 12 & 5 = 9;

4.~:

The function of the 'inversion' operator ~ is to invert the digits: all 0s are set to 1, and 1s are set to 0. The ratio is as follows:

12 in binary is 1100

The negation operation is 10000000 00000000 00000000 00001101

Then the complete formula is
~12 = -13

5.<<:

Left shift is to shift all the digits of a number to the left by a number of digits. The ratio is as follows:

12 in binary is 1100

Then the binary of 12 << 1 is 11000, then the complete operation formula is 12 << 1 = 24;

6.>>:

Shift right is to move all the digits of a number to the right by a number of digits. The ratio is as follows:

12 in binary is 1100

Then the binary of 12 >> 1 is 0110. Then the complete formula is 12 >> 1 = 6;

7.>>>:

Unsigned right shift one bit. Insufficient 0 is added. Example ratios are as follows:

12 in binary is 1100

Then the binary of 12 >> >1 is 0110, then the complete operation formula is 12 >> 1 = 6;

Digression: Bit operation is a binary-based operation. It involves knowledge including original code, inverse code, and complement code. Here is a small explanation.

for the original code. It is the binary representation of the current number. For example, the original code of -1 is 1000 0001.

For one's complement, the complement of a positive number is itself. The complement of a negative number is the binary reserved sign bit. The remaining bits are inverted, for example, the complement of -1 is 1111 1110;

For the complement code, the complement, complement, and original code of positive numbers are the same, and the complement code of negative numbers is +1 based on the complement code, for example, the complement code of -1 is 1111 1111.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325559008&siteId=291194637