What is the difference between the operator >> and >>>?
-
http://en.wikipedia.org/wiki/Bitwise_operation#Shifts_in_C.2C_C.2B.2B_and_Java – Gopi Jan 06 '10 at 11:48
-
That's eight... Hey come on. SO is a great place to learn but you are going to learn more, faster and better if you start here: http://java.sun.com/docs/books/tutorial/java/index.html – Fredrik Jan 06 '10 at 11:50
4 Answers
>>>
right shifts and fills with 0 at the left end, while >>
fills with the sign bit.
This makes a difference for the signed integral types (i.e. all but byte
), where a negative value has a set sign bit.

- 66,391
- 18
- 125
- 167
>>
Signed right shift operator and >>>
unsigned right shift operator
The right shift
>>
operator shifts the left operand to the right side with sign extension by the number of bits specified by its right operand. This means that a value at n place gets shifted to the right causing the n high order bits that contains the same value as that of unshifted value. This operator never throws an exception.
The unsigned right shift
>>>
operator shifts a zero into the leftmost position however the leftmost position after ">>" depends on sign extension.

- 184,426
- 49
- 232
- 263
the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
From http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html

- 53,737
- 19
- 129
- 165
Here is an explanation with examples:
http://www.roseindia.net/java/master-java/bitwise-bitshift-operators.shtml
>>
fills in the sign the sign bit on the left (i.e. fills in 1 for negative values, 0 for positive), whereas >>>
doesn't (always 0). This is convenient when shifting negative values. There is no <<<
, since the sign bit is on the left and <<
thus already behaves like <<<
would (filling in zeros, nothing else).

- 11,904
- 14
- 72
- 127