Zero. Learning objectives of this lecture
- Understand the meaning of bit operation
- Master the use of bit operation
1, Bit operator concept
- Bit operators are symbols that operate on each bit of binary numbers. They operate specifically on numbers 0 and 1.
2, Bitwise operator usage
(1) Classification description table
operator | operation | example | result |
---|
& | Bitwise AND | a & b | The result of "and" operation for each bit of a and b |
| | Bitwise OR | a | b | The result of "or" operation for each bit of a and b |
~ | Reverse | ~ a | The result of "not" operation for each bit of a |
^ | Bitwise XOR | a ^ b | The result of "XOR" operation for each bit of a and b |
<< | Shift left | a << b | Move a to the left by b, and fill the space on the right with 0 |
>> | Shift right | a >> b | Move a right to bit b, discard the moved out bit, and fill the highest bit on the left with 0 or 1 |
>>> | unsigned right shift | a >>> b | Move a right to bit b, discard the moved out bit, and fill the highest bit on the left with 0 |
- Bitwise and operation truth table
- Truth table of inverse operation
(2) Precautions
The essence of bit operators is to operate on binary numbers 0 and 1. When using bit operators, the operands will be converted into binary numbers for bit operation, and then the results will be converted into the desired binary numbers. Where 1 means true and 0 means false.
(3) Case demonstration
1. Bitwise AND
![](/images/think/e1513ab4da9f7834ae2996bb4321c0d6.jpg)
2. Bitwise OR
![](/images/think/0a2da5d590a75c36f28e42e5893ee3dd.jpg)
3. Bit by bit inversion
![](/images/think/0815bb74474f10c38e8ea5e28606ab9e.jpg)
4. Bitwise XOR
![](/images/think/182f9a2d15e31d2bcdcc5a5353a209e5.jpg)
5. Shift left
![](/images/think/0bf05998f07c2b4ba531b1d3e7cf6fec.jpg)
6. Shift right
![](/images/think/2d363d9b6dba513a2162a6991d88ed82.jpg)
7. Unsigned right shift
![](/images/think/12494aee08522ed115726bf1668995b2.jpg)
8. Demonstrate various bit operations
![](/images/think/83d4f324563e00e84573c85fbd857db1.jpg)
package net.hw.lesson05;
/**
* Function: demonstrate bit operation
* Author: Wei Hua
* Date: March 22, 2020
*/
public class Example509 {
public static void main(String[] args) {
int a = 0b00000110;
int b = 0b00001011;
// Bitwise AND
System.out.println(Integer.toBinaryString(a) + " & " + Integer.toBinaryString(b) + " = " + Integer.toBinaryString(a & b));
// Bitwise OR
System.out.println(Integer.toBinaryString(a) + " ! " + Integer.toBinaryString(b) + " = " + Integer.toBinaryString(a | b));
// Bit by bit inversion
System.out.println("~" + Integer.toBinaryString(a) + " = " + Integer.toBinaryString(~a));
// Bitwise XOR
System.out.println(Integer.toBinaryString(a) + " ^ " + Integer.toBinaryString(b) + " = " + Integer.toBinaryString(a ^ b));
// Shift left
System.out.println(Integer.toBinaryString(b) + " << 1 = " + Integer.toBinaryString(b << 1));
// Shift right
System.out.println(Integer.toBinaryString(b) + " >> 1 = " + Integer.toBinaryString(b >> 1));
// unsigned right shift
System.out.println(Integer.toBinaryString(b) + " >>> 1 = " + Integer.toBinaryString(b >>> 1));
}
}
- Run the program and view the results
![](/images/think/57d76285505c843035e5bb338fb387c2.jpg)
9. Demonstrate the difference between shift right and unsigned right
How can we reflect the difference between right shift and unsigned right shift?
![](/images/think/c5369665fa4545f0ff4ca11f6b2685d9.jpg)
package net.hw.lesson05;
/**
* Function: demonstrate the difference between right shift and unsigned right shift
* Author: Wei Hua
* Date: March 22, 2020
*/
public class Example510 {
public static void main(String[] args) {
int a = 0b00000111111111111111111111111001; // Positive number
int b = 0b10000111111111111111111111111001; // negative
// Shift right
System.out.println(Integer.toBinaryString(a) + " >> 1 = " + Integer.toBinaryString(a >> 1));
// unsigned right shift
System.out.println(Integer.toBinaryString(a) + " >>> 1 = " + Integer.toBinaryString(a >>> 1));
// Shift right
System.out.println(Integer.toBinaryString(b) + " >> 1 = " + Integer.toBinaryString(b >> 1));
// unsigned right shift
System.out.println(Integer.toBinaryString(b) + " >>> 1 = " + Integer.toBinaryString(b >>> 1));
}
}
Run the program and the results are as follows:
![](/images/think/535e195e5892de65830fd0c5af05fbf3.jpg)
The highest bit is the sign bit, 0 is a positive number and 1 is a negative number.
For positive numbers, a right shift results in the same way as an unsigned right shift.
For negative numbers, the result of shift right is different from that of unsigned right.
(4) Classroom practice
- Program and calculate the following bit operation results
1,23 & 34
2,23 | 34
3,23 ^ 34
4,~23
5,32 >> 2
6,-32 >> 2
7,32 >>> 2
8,-32 >>> 2
9,4 << 3
![](/images/think/0a6ce09972018fa71ae7dd105ac985ea.jpg)
You can compare the bit operation of Python:
![](/images/think/de9b415ad6acd74742bc72e048f8272f.jpg)
As you can see, except that there is no unsigned right shift operator in Python, other bit operators are exactly the same as Java.