The Shift Operators and
suggest changeThe Java language provides three operator for performing bitwise shifting on 32 and 64 bit integer values. These are all binary operators with the first operand being the value to be shifted, and the second operand saying how far to shift.
- The
<<
or left shift operator shifts the value given by the first operand leftwards by the number of bit positions given by the second operand. The empty positions at the right end are filled with zeros. - The ‘>>’ or arithmetic shift operator shifts the value given by the first operand rightwards by the number of bit positions given by the second operand. The empty positions at the left end are filled by copying the left-most bit. This process is known as sign extension.
- The ‘>>>’ or logical right shift operator shifts the value given by the first operand rightwards by the number of bit positions given by the second operand. The empty positions at the left end are filled with zeros.
Notes:
- These operators require an
int
orlong
value as the first operand, and produce a value with the same type as the first operand. (You will need to use an explicit type cast when assigning the result of a shift to abyte
,short
orchar
variable.) - If you use a shift operator with a first operand that is a
byte
,char
orshort
, it is promoted to anint
and the operation produces anint
.) - The second operand is reduced modulo the number of bits of the operation to give the amount of the shift. For more about the mod mathematical concept, see Modulus examples.
- The bits that are shifted off the left or right end by the operation are discarded. (Java does not provide a primitive “rotate” operator.)
- The arithmetic shift operator is equivalent dividing a (two’s complement) number by a power of 2.
- The left shift operator is equivalent multiplying a (two’s complement) number by a power of 2.
The following table will help you see the effects of the three shift operators. (The numbers have been expressed in binary notation to aid vizualization.)
Operand1 | Operand2 | <<
| >>
| >>>
| |
—— | —— | —— | —— | —— | —— |
0b0000000000001011 | 0 | 0b0000000000001011 | 0b0000000000001011 | 0b0000000000001011 | no change |
0b0000000000001011 | 1 | 0b0000000000010110 | 0b0000000000000101 | 0b0000000000000101 | |
0b0000000000001011 | 2 | 0b0000000000101100 | 0b0000000000000010 | 0b0000000000000010 | |
0b0000000000001011 | 28 | 0b1011000000000000 | 0b0000000000000000 | 0b0000000000000000 | |
0b0000000000001011 | 31 | 0b1000000000000000 | 0b0000000000000000 | 0b0000000000000000 | |
0b0000000000001011 | 32 | 0b0000000000001011 | 0b0000000000001011 | 0b0000000000001011 | 32 mod 32 is 0 |
… | … | … | … | … | |
0b1000000000001011 | 0 | 0b1000000000001011 | 0b1000000000001011 | 0b1000000000001011 | no change |
0b1000000000001011 | 1 | 0b0000000000010110 | 0b1100000000000101 | 0b0100000000000101 | note sign extension for ‘>>’.
0b1000000000001011 | 2 | 0b0000000000101100 | 0b1110000000000010 | 0b00100000000000100 | note sign extension for ‘>>’.
0b1000000000001011 | 31 | 0b1000000000000000 | 0b1111111111111111 | 0b0000000000000001 | note sign extension for ‘>>’. |
There examples of the user of shift operators in http://stackoverflow.com/documentation/java/1177/bit-manipulation#t=201610101439344327372