why is a negative shift value illegal?

Tim Peters tim.one at home.com
Sat Jan 13 19:03:51 EST 2001


[Chris Ryland]
> ...
> Why shouldn't negative shifts just mean a shift in the opposite
> direction?  To avoid confusion? Becuse there's only way way to
> do things in Python?
>
> E.g., 16 << -2 seems like it might well be defined as 16 >> 2.

Because we don't have signed zeroes in Python:  in

    16 << 0
vs
    16 << -0

we couldn't tell whether you wanted us to leave 16 alone from the left or
from the right <wink>.

Sorry, couldn't resist that.  Simpler answer is "why would they?".  shifts
don't work that way in most other languages; seems of marginal utility at
best; and would slow down all Python shifts because Python is implemented in
C and we could no longer use the native C shift (well, we can't anyway,
because C doesn't define the behavior of signs on right shifts; or shifts
greater than or equal to the bit size in either direction; and Python does
define those cases -- but yet another test in the implementation maze sure
wouldn't speed things up).

if-you-want-to-shift-right-then-shift-right-ly y'rs  - tim





More information about the Python-list mailing list