Tuesday, May 16, 2006

Java's unsigned bit shift right

Not having had the need for bit-twiddling in Java, I just now learned that Java has an "unsigned shift right" operator, >>>. The signed operator >> retains the sign bit (for turning -8 into -4, for example) and >>> shifts in a 0 to the most significant bit. This unsigned right shift operator has been part of the language since 1.0. I suppose the need for the two operators arises because all Java's integral types are signed, meant to support both signed and unsigned operations.

1 comment:

jfklein said...

More information about working with Java and unsigned types can be found at http://darksleep.com/player/JavaAndUnsignedTypes.html.

I particularly found the "Why no unsigned types?" section and its link to a James Gosling interview interesting.