Demo and summary of Shift Operators in java, and operator demo
First, there are three types of shift operators: byte, short, char, int, and long.
<Left shift operator, which indicates to move the binary data of the left operand to the left * bit. After moving, the vacant bit is filled with 0, and the excess bit is discarded. (Equivalent to the Npower of 2)
> Right shift operator. How many digits does the binary data erase after the binary data is shifted * to the right? (This is a good decision, but I personally understand it) (equivalent to the power of Npower except 2)
>>> The unsigned right shift operator fills the left-side vacant space with 0, regardless of whether the highest bit before the move is 0 or 1.
Here is a demo to help you understand:
Public static void main (String [] args) {int a = 16; int B = a <2; // shift left 2, equivalent to the 2nd power of 16*2, that is, 16*4 int c = a> 2; // shifts 2 to the right, equivalent to the 2nd power of 16/2, that is, 16/4 System. out. println ("the binary data of a is:" + Integer. toBinaryString (a); System. out. println ("binary data after a shifts left:" + Integer. toBinaryString (B); System. out. println ("a right-shifted binary data:" + Integer. toBinaryString (c); System. out. println ("the value after a shifts left is:" + B); System. out. println ("the value after right shift of a is:" + c );}}
View the running result ,:
First, the binary data of 16 is 10000;
Shift two places left, 10000 to 1000000;
Shifts two places to the right, and 10000 to 100;
After the binary data is converted to normal data,
The value after a shifts left is 64, which is 16x2 to the power of 2 (16x4 ).
The value after the right shift of a is 4, which is the 2nd power of 16/2 (16/4 ).
After reading this demo, I can understand whether the shift operator is much simpler?