There are three kinds of shift operators in Java
<<: Left-shift operator, NUM << 1, equivalent to num multiplied by 2
>>: Right shift operator, NUM >> 1, equivalent to num divided by 2
>>>: Unsigned right shift, ignoring symbol bit, vacancy is 0
Let's take a look at how these shift operations are used
Copy Code code as follows:
/**
*
*/
Package com.b510.test;
/**
* @author Jone Hongten
* @create Date:2013-11-2
* @version 1.0
*/
public class Test {
public static void Main (string[] args) {
int Number = 10;
//RAW number binary
printinfo ( number);
number = number << 1;
//Move left one
printinfo ( number);
number = number >> 1;
//Right-move one
printinfo ( number);
}
/**
* Output binary number of an int
* @param num
*/
private static void printinfo (int num) {
System.out.println (integer.tobinarystring (num));
}
}
The results of the operation are:
1010
10100
1010 We align the above results:
43,210 digits--------
10,100 in: 10 original number
101,000 binary: 20 left one number = number << 1;
10,100 in: 10 to the right one number = number >> 1;
Look at the demo above, now is not on the left and right move to understand a lot of it
For:>>>
Unsigned right shift, ignoring sign bit, empty space with 0
Value >>> num--num Specifies the number of digits to shift the value of.
The rule of unsigned right shift only remembers one point: the symbol bit extension is ignored, the 0 complement highest bit unsigned right shift operator >>> is only meaningful for 32-bit and 64-bit values