If you want to get to know Java, bitwise operators, you must first get to know the binary arithmetic, before there is a detailed look at the binary arithmetic - binary and decimal conversion
In Java bitwise operators are: & (bitwise and), | (bitwise OR), ^ (bitwise exclusive or) >> (shift right), << (shift left), ~ (inverted), >>> (unsigned right shift)
Let's introduce one by one:
& (Bitwise AND)
int i = 5&2; int j = 7&3; System.out.println("i="+i);//i=0 System.out.println("j="+j);//j=3
Bitwise rules and is converted into a binary first sides, and then calculate the final value, the arithmetic rule " every change 0 0 ", i.e., 1 & 1 = 0 = 1,1 & 0,0 & 0,0 & 1 = 0 = 0
Binary Binary 10,5 101,2 5 & 2 & 10 is the 101, 010 & 0,101 complement insufficient bits, in accordance with the rules of arithmetic obtain the final value of 0, is 0 decimal
Binary A of 7 111,3 11,7 & 3 111 011 that is, the final value obtained based on the calculation rules 011, is converted to decimal 3
| (Bitwise OR)
int i = 6|2; int j = 5|3; System.out.println("i="+i);//i=6 System.out.println("j="+j);//j=7
Bitwise OR operation on both sides of the rule is first converted to binary, arithmetic rule " every 1 becomes 1 ", i.e. 1 | 1 = 1,1 | 0 = 1,0 | 1 = 1,0 | 0 = 0
Binary binary 6 110,2 10,6 | 2 is the 110 | 010, the final value obtained based on the calculation rules 110, is decimal 6
Binary Binary 11,5 101,3 of 5 | 3 is the 101 | 011, the final value obtained based on the calculation rules 111, that is, the decimal
^ (Bitwise exclusive or)
int i = 7^3; int j = 5^2; System.out.println("i="+i);//i=4 System.out.println("j="+j);//j=7
Bitwise exclusive or, by definition not the same as is the digit 1 , the calculation rule for 1 = 1 ^ 0,1 ^ 1,0 ^ 1 = 0 = 0 = 0 ^ 1,0
7 111,3 binary binary equivalent of 11, 7 ^ 3 111 011, based on the calculation result of rule 100, decimal 4
5 101,2 binary binary equivalent of 10, 5, 2 ^ 101 ^ 010, a result 111, a decimal 7
<< (left)
int i = 2<<3; int j = 5<<2; System.out.println("i="+i);//i=16 System.out.println("j="+j);//j=20
Left is the effective value of the binary values to the left, shift right value is the number of bits of "<<" symbols << 2 3 2 is the effective value of the binary three to the left, the right complement 0
2 binary 0000 0010, 0001 0000 for the left by 3 bits, decimal 16
5 binary 00000101, 00010100 left by 2 bits, decimal 20
>> (right)
int i = 9>>2; int j = 15>>3; System.out.println("i="+i);//i=2 System.out.println("j="+j);//j=1
Right now is the opposite of the left, to the right is the N-bit shift to the right, left complement positive number 0, 1 left up negative
9 binary 00001001, right by 2 bits, 00000010, decimal 2
15 0000 1111 binary, right 3, 0000 0001, decimal 1
~ (Inverted)
int i = ~5; int j = ~8; System.out.println("i="+i);//i= -6 System.out.println("j="+j);//j= -9
After negated is converted to a binary 1 to 0,0 to 1
5 0000 0000 0000 0000 binary 0,000,000,000,000,101, negated 1,111,111,111,111,111 1,111,111,111,111,010, the decimal value is inverted -6
In fact, there is a better understanding of the embodiment, a score imaginary axis, and the intermediate point 0 to -1 is the inverted value of the center point of the same positions of the overlap folding, simply invert inverse number minus one is
>>> (unsigned right shift)
int i = 5>>>2; int j = -5>>>2; System.out.println("i="+i);//i= 1 System.out.println("j="+j);//j= 1073741822
Unsigned right shift in the calculation of the number of positive right and there is no difference, except that the calculated negative
-5 binary is 1,111,111,111,111,111 1,111,111,111,111,011 (before converting a negative way already mentioned), the right two 00111111111111 1,111,111,111,111,111 10 left fill 0, but left up in the >> 1
>>: Signed right shift operation, the digital calculation if participation is positive, then the high bit 0, when high level is negative to the left of S.1
>>>: unsigned right shift operation, regardless of the numbers involved in computing is positive or negative, we are left high up 0