First of all, there are three types of shift operators, and their operation types only support five types: byte/short/char/int and long.
<< left shift operator, which means to move the binary data of the operand on the left to the left * bit, after the shift, the vacant bit is filled with 0, and the excess bits are discarded. (Equivalent to the n power of multiplying 2)
>> Right shift operator, the binary data moves * bits to the right, just erase how many bits after its binary data? (It's pretty good to set here, but I understand it like this) (equivalent to the n power of 2)
>>> The unsigned right shift operator, regardless of whether the highest bit before the shift is 0 or 1, the empty bits generated on the left after the right shift are filled with 0.
Let's use a demo to help understand:
public static void main(String[] args){ int a = 16; int b = a << 2;//Transfer 2 to the left, equivalent to the power of 2 of 16 * 2, that is, 16 * 4 int c = a >> 2;//Transfer 2 to the right, equivalent to the power of 2 of 16 / 2, that is, 16 / 4 System.out.println("a's binary data is: " + Integer.toBinaryString(a)); System.out.println("a's binary data is: " + Integer.toBinaryString(b)); System.out.println("a's binary data is: " + Integer.toBinaryString(b)); System.out.println("a's binary data is: " + Integer.toBinaryString(c)); System.out.println("a's value after shifting left is: " + b); System.out.println("a's value after shifting right is: " + c); }}Check the running results as shown in the figure:
First, the binary data of 16 is: 10000;
Move two bits left, and 10000 becomes 1000000;
Move two digits right, and 10000 becomes 100;
After converting the obtained binary data into ordinary data,
The value after the left shift is 64, which is 16 * 2 to the power of 2 (16 * 4).
The value after the right shift is 4, which is 16/2 to the power of 2 (16/4).
Is it much easier to understand the shift operator after reading this demo?
The above demo and summary of shift operators (recommended) in Java is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.