Transfer from http://aijuans.iteye.com/blog/1850655
public class Test {public static void main (string[] args) {//1, Shift Left (<<)//0000 0000 0000 0000 0000 0000 0000 0101 After the left 2-bit, the low-0:////0000 0000 0000 0000 0000 0000 0001 0100 is converted to 10-20system.out.println (5 << 2);//operation result is 20//2, right shift ;>) high fill sign bit//0000 0000 0000 0000 0000 0000 0000 0101 Then move right 2 bits, high fill 0://0000 0000 0000 0000 0000 0000 0000 0001system.out. println (5 >> 2);//Run result is 1//3, unsigned Right shift (>>>) high fill 0//For example 5 is converted to binary: 0101 is reversed plus 1 is 1011//1111 1111 1111 1111 1111 1 111 1111 1011//We shifted the 5 right shift 3 bits, 5 to right 3 bits and unsigned right shift 3 bits: SYSTEM.OUT.PRINTLN (5 >> 3);//The result is 0system.out.println ( -5 >> 3 );//result is -1system.out.println ( -5 >>> 3);//result is 536870911//4, Bit and (&)//bit with: the first operand of the nth position in the second operand if all is 1, Then the result of Nth is also 1, otherwise 0system.out.println (5 & 3);//result is 1system.out.println (4 & 1);//result is 0//5, bit or (|) The nth bit of the first operand is the nth digit of the second operand as long as one is 1, then the nth of the result is also 1, otherwise 0system.out.println (5 | 3);//result is 7//6, bit XOR or (^)/ The nth position of the first operand is the opposite of the nth bit of the second operand, then the nth of the result is also 1, otherwise 0 System.out.println (5 ^ 3);//result is 6//7, bit non (~)//The nth bit of the operand is 1, then the nth bit of the result is 0, and vice versa. SYSTEM.OUT.PRINTLN (+);//result is-6}}
Java bit arithmetic