Re: left shift
Ulrich Eckhardt wrote:
devnew@gmail.com wrote:
i am relatively new to python..was checking out some bit shifting..
when i do
255<< 24 i get 4278190080
..which is correct, assuming a left-shift means that the value is multiplied
by 2 raised to the power of 24, although it is not common because usually
the behaviour is inherited from the CPU which uses a limited amount of
digits.
whereas in java or c it gives -16777216
..which may or may not be correct, depending on the definition. At least for
C, I believe that this causes "undefined behaviour", which is why you
should avoid using shift operations on signed types there, rather use
unsigned types. I guess that Java's VM is defined so that its shift
operations assume twos complement representations of signed integers. Can
anyone clarify that?
....
Java operator behavior is specified in the Java Language Specification,
so the same rules apply regardless of whether Java is implemented using
the JVM or not.
">>" does two's complement sign extension. See the JLS, 15.19 Shift
Operators, at
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.19
Java does have another right shift, ">>>", that does zero fill and has
the effect of an unsigned right shift.
Patricia