There are three kinds of shift operators in Java
<<: Left shift operator, num << 1, equals num multiplied by 2
>>: Right shift operator, NUM >> 1, equivalent num divided by 2
>>>: Unsigned Right shift, ignore sign bit, empty 0
Let's see how these shift operations are used.
1 /** 2 * 3*/ 4Package com.b510.test;5 6 /** 7 * @author Jone hongten 8 * @create date:2013-11-2 9 * @version 1.010*/ One Public classTest { A - Public Static voidMain (string[] args) { - intNumber =Ten; the //Binary binary of primitive number -printinfo (number); -Number = number <<1; - //Move left one +printinfo (number); -Number = number >>1; + //Move Right One Aprintinfo (number); at } - - /**26 * Outputs a binary number of int * @param num28*/ in Private Static voidPrintinfo (intnum) { -System. out. println (integer.tobinarystring (num)); to } +}
The result of the operation is:
1010101001010
Let's align the results above:
43210 Number of digits
--------1010 Decimal: original number number10100 decimal: shift left one digit = number << 1; 1010 Decimal: Ten right shift one number = number >> 1;
Look at the above demo, now is not the left shift and right shift to understand a lot of it
For:>>>
Unsigned right shift, ignore sign bit, empty 0
Value >>> num--num Specifies the number of bits to shift the value to move.
Unsigned right-shift rules only remember one point: ignoring the sign bit extension, the 0-bit unsigned right-shift operator >>> is only meaningful for 32-bit and 64-bit values
:<<,>>,>>> summary of shift operators in Java