2

I recently used the shift operators in Java and noticed that the >> operator does not have the same meaning as >> in C. In Java >> is Signed shift that keeps the first bit at the same value. In Java the equivalent to C shift is the >>> operator. The left shift operator (<<) is the same as in C and just shifts ignoring the first bit.

The things I wondered are

  • Why make this change?
  • Why is the notation not consistent so >> and << are signed shift and >>> and <<< are unsigned?
  • Is there any use for a signed shift operator?
Russ Hayward
  • 5,617
  • 2
  • 25
  • 30
nist
  • 1,706
  • 3
  • 16
  • 24
  • It's necessary because Java has no unsigned types. Btw it is already as you suggest: >> is signed and >>> is unsigned. There is no <<< because it would do the same thing as << anyway. – harold Mar 15 '12 at 15:32
  • AFAIK `a >> b` is a signed shift in C for signed types too. http://en.wikipedia.org/wiki/Bitwise_operation#Shifts_in_C.2C_C.2B.2B.2C_C.23 – Peter Lawrey Mar 15 '12 at 15:37
  • No in C all the shift opperations are machine dependent but most of the time >> is unsigned – nist Mar 15 '12 at 15:43

3 Answers3

2

There is never any need for a sign-aware left shift, since 2:s complement representation stores the sign in the most significant bit.

There's no difference between a value shifted one bit to the left in some kind of "sign-aware" manner, there's nothing you can do differently. Shift the bits to the left, insert a 0 in the least significant bit, and you're done.

With signed numbers, shifting right is not so clear-cut, which is why there are two operators.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • But any kind of shifting will change the most significant bit. Left shifting 0111 (7) using signed ints will result in 1110 (-2) – nist Mar 15 '12 at 15:38
  • That's correct. _Any_ kind of shifting will do that. So it doesn't matter which kind you use. – Louis Wasserman Mar 15 '12 at 15:42
  • Well, yes, what do you propose the result of such a shift should be? You can't represent 14 (= 7 << 1) with a 4 bit signed integer, so whatever result you get is going to be garbage anyway. – AVH Mar 15 '12 at 15:43
2

As far as I know the meaning of >> and >>> has always been the same in Java.

Why make this change?

Machine independence. The meaning of >> is somewhat implementation dependent in C.

ldav1s
  • 15,885
  • 2
  • 53
  • 56
  • +1 Java was created to abstract from the underlying hardware, so each operator work the same way on every computer. – Aaron Digulla Mar 15 '12 at 15:48
  • @AaronDigulla, exactly. The JVM (Java Virtual Machine) is that abstract hardware that allows Java to "Write once, run everywhere" (or so they marketed :-). – ldav1s Mar 15 '12 at 22:42
1

Signed left shift and unsigned left shift are exactly equivalent, so there's no need to have a separate operator.

On the other hand, Java has no unsigned types, so it can't depend on the type to figure out what shift to use.

For reference, I think you have it backwards -- >> is signed right shift, >>> is unsigned right shift.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413