tags:

views:

98

answers:

3

What is the difference between the operator >> and >>>?

+3  A: 

>> Signed right shift operator and >>> unsigned right shift operator

Signed 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.

Unsigned Right Shift Operator

The unsigned right shift >>> operator shifts a zero into the leftmost position however the leftmost position after ">>" depends on sign extension.

rahul
+3  A: 

>>> 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.

Carl Smotricz
+2  A: 

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

Dan Dyer