There is a class of operators in Java that operate on binary. They are &, |, ^, ~, >>, <<, >>> several bit operators. Whether the initial value is based on what kind of binary, it will be converted into binary bits
Operation. The following is a brief explanation of these operators.
&:
The function is to perform the bitwise AND operation of the operator on both sides of a binary expression. This operation is in the same number of bits (bit) as the unit. The rules for the operation are: only two operands are 1 o'clock. The output is only 1. Whether
is 0, the scale is as follows:
12 binary is 1100
5 binary is 0101
Then the binary of the & 5 is 0100, then the complete arithmetic is 5 = 4;
|
The function is to perform a bitwise ' or ' operation on both sides of the operator in binary representations. This operation is in the same number of bits (bit) as the unit.
The rule of action is that only if two operands are 0 o'clock, the result of the output is 0.
otherwise 1, the scale is as follows:
12 Binary is 1100
5 binary is 0101
Then 12 | The binary of 5 is 1101. Then the complete expression is & 5 = 13;
^ :
The function is to perform an ' XOR ' operation on two sides of the operator with binary representation of the operands, and this operation is in the same bit (bit) of the number.
The rules for an XOR operation are: only if the two operands are not the same. corresponding to the
lose The result is only 1, otherwise 0, the ratio is as follows:
12 Binary is 1100
5 binary is 0101
Then 12 | The binary of 5 is 1001. Then the complete expression is & 5 = 9;
~ :
the function of the ' inverse ' operator is to reverse the numbers: all 0 is 1,1 is 0, and the ratio is as follows:
12 Binary is 1100
The counter operation is 10000000 00000000 00000000 00001101
The complete expression is ~12 = 13
<< :
The left shift is to move the entire number of digits to the left by a number of bits , as shown in the following scale:
12 Binary is 1100
Then the binary of the << 1 is 11000, then the complete arithmetic is 1 = 24;
>> :
The right shift is to move the entire number of digits to the right by a number of bits , as shown in the following scale:
12 Binary is 1100
then the binary of >> 1 is 0110. Then the complete expression is >> 1 =6;
>>> :
unsigned Right move one . insufficient complement of 0. The scale is as follows:
12 Binary is 1100
then the binary of >> >1 is 0110, then the complete arithmetic is 1 = 6;
Off-topic: bit operation is a binary-based operation, involving the knowledge contains the original code, anti-code, complement, here to do a small explanation,
For the original code. is the binary representation of the current number, such as 1 of the original code is 1000 0001.
For the inverse code, the inverse of the positive number is itself. The inverse of a negative number is a binary reserved symbol bit. The inverse of the remaining bits, for example 1, is 1111 1110;
For the complement, positive number of anti-code, complement, the original code is the same, negative complement is in its anti-code based on +1, such as 1 of the complement is 1111 1111.
In the original code of the JDK. Many of the initial values are computed by bit operations, and bit operations have many characteristics that can play a role in linearly growing data. And for some operations, bit operation is the most direct and simplest method.
Java bit operators in-depth understanding