3

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

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
  • 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 Answers4

5

>>> 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
  • 66,391
  • 18
  • 125
  • 167
4

>> 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
  • 184,426
  • 49
  • 232
  • 263
2

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
  • 53,737
  • 19
  • 129
  • 165
0

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

scravy
  • 11,904
  • 14
  • 72
  • 127