1. &("and") AND operation
int a = 0b1000; //0b1000 is the binary representation of 8 int b = 0b1011; //0b1011 is the binary representation of 11 int c = a & b; // AND bitwise operation on a and b System.out.println(c);//The result c is 8, which is 0b1000
The specific operation logic is as follows: only when the corresponding bits of the two operands are 1, the result of the & operation is 1, otherwise it is 0.
a 0b1000
b 0b1011
-----------
c 0b1000
2. | ("or") OR operation
int a = 0b1000; //0b1000 is the binary representation of 8 int b = 0b1011; //0b1011 is the binary representation of 11 int c = a | b; // OR bitwise operation on a and b System.out.println(c);//The result c is 11, which is 0b1011
The specific operation logic is as follows: as long as one of the corresponding bits of the two operands is 1, the | operation result is 1, otherwise it is 0.
a 0b1000
b 0b1011
-----------
c 0b1011
3. ^( "xor") XOR operation
int a = 0b1000; //0b1000 is the binary representation of 8 int b = 0b1011; //0b1011 is the binary representation of 11 int c = a ^ b; // XOR bit operation on a and b System.out.println(c);//The result c is 3, which is 0b0011
The specific operation logic is as follows: as long as the corresponding bits of the two operands are the same, the result of the ^ operation is 0, otherwise it is 1.
a 0b1000
b 0b1011
-----------
c 0b0011
4. ~("not" ) NOT operation
int a = 0b1000; //0b0000 1000 is the binary representation of 8 int c = ~a; //Do a non-bit operation on a System.out.println(c);//The result c is -9, which is 0b1000 1001
The specific operation logic is as follows: ~The operation rule is bit-wise inversion, which is the complement code at this time. It needs to be converted into the original code, that is, the bit-wise inversion is first, except for the sign bit, and then the last bit is +1.
0b0000 1000 Bit-wise inversion first , get the following.
0b1111 0111 is the complement code at this time. It needs to be converted into the original code.
0b1000 1000 is bitwise inverted first, except for the sign bit
.