1

I found this java question on the internet and had some questions about it.

Which statements are accurate:

  • a) >> performs signed shift while >>> performs an unsigned shift.
  • b) >>> performs a signed shift while >> performs an unsigned shift.
  • c) << performs a signed shift while <<< performs an unsigned shift.
  • d) <<< performs a signed shift while << performs an unsigned shift.

I'm a little unsure what a signed shift is, does it mean that it retains the sign of the binary number regardless of what happens in the shift itself (which would make the most sense to me) or does it mean that the MSB does not change unless it is overwritten in the shift operation itself.

so

  • a) true: regardless of how many shifts using >> you make, the MSB is always retained as its original therefore signed? Whereas >>> will always overwrite the MSB with a 0, and therefore unsigned ?
  • b) false, because of above explanation
  • c) not sure, because the first bit could be overwritten with the << shift operation and therefore doesn't retain its sign?
  • d) not sure again.
code-jaff
  • 9,230
  • 4
  • 35
  • 56
LocalMagic
  • 107
  • 1
  • 7
  • See this similar question: http://stackoverflow.com/questions/2244387/bitwise-shift-operators-signed-and-unsigned – Dave Aug 14 '12 at 14:03

5 Answers5

2

Another description: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

From the article: "The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension."

Dave
  • 13,518
  • 7
  • 42
  • 51
  • to clarify, for >>, if the number is negative, a 1 is shifted into the most significant bit in order to keep the number negative. This guarantees that the sign will not change when using this type of shift. – Matt Aug 14 '12 at 14:16
1

">>" performs a signed shift which fills in the new bits with whatever the leftmost bit is. The leftmost bit determines if the number is negative or positive. 0 for positive and 1 for negative. For example,

>> 1
10111100 becomes 11011110
the leftmost bit is a 1, so the new bits after the shift become ones

>> 1
01110011 becomes 00111001 since the leftmost bit is a 0

">>>" performs an unsigned shift which means the new bits are always filled with zeroes after the shift. For example,

>>> 1
10111100 becomes 01011110
the new bits are filled in as zeroes no matter what the leftmost bit is


enter code here
theDazzler
  • 1,039
  • 2
  • 11
  • 27
0

Look in Java Language Specification: http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.19

Marc
  • 1,900
  • 14
  • 22
0

does it mean that it retains the sign of the binary number regardless of what happens in the shift itself (which would make the most sense to me) or does it mean that the MSB does not change unless it is overwritten in the shift operation itself.

That's the same thing. ;)

Whereas >>> will always overwrite the MSB with a 0, and therefore unsigned

If you do -1 >>> 0 it will still be negative, but its basically right ;)

left shift is not signed or unsigned, is just a left shift and as you say it may or may not change the sign.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • So a signed shift would be either << or >> and means that either the MSB is kept as it is if shifting >> for example the MSB is kept at '1', and even if the MSB is overwritten due to the shift it is still called a Signed Shift? – LocalMagic Aug 14 '12 at 14:07
  • and >>> or <<< will always be considered unsigned because it just sets the MSB to '0'? – LocalMagic Aug 14 '12 at 14:09
  • That would be true, but `<<` doesn't preserve the sign so if anything its an unsigned shift. e.g. `x << 1 >>> 1` will be x if the top bit is not lost but `x << 1 >> 1` will not be x if the top bit is not lost. – Peter Lawrey Aug 14 '12 at 14:10
  • There is no such thing as `<<<` but you are right about `>>>` provided it shifts at all. e.g. `-1 >>> 64` is -1 i.e. negative. – Peter Lawrey Aug 14 '12 at 14:11
  • So only a) is true, because >> always retains the sign, and >>> does not as it set it to 0. The others b) c) and d) are not accurate – LocalMagic Aug 14 '12 at 14:21
0
<<  Signed left shift      op1 << op2 
>>  Signed right sift      op1 >> op2 
>>> Unsigned right shift   op1 >>> op2 
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75