Does Python need a '>>>' operator?
Martin v. Loewis
martin at v.loewis.de
Sat Apr 13 19:49:02 EDT 2002
"Ken Peek" <Ken.Peek at SpiritSongDesigns.comNOSPAM> writes:
> The merging of the 'long int' and the 'int' type is
> precisely WHY there _should_ be a '>>>' operator. Did you
> read ALL of my post?
Can you elaborate? You explained that the >>> fills in zeroes for
32-bit numbers that have the highest bit set. With long integers, you
don't need that: If you have a number that has the 2**31 bit set, it
may still be a positive number, and right-shifting will fill in the
zero bit. So no need for a >>> operator.
>
> | One might question whether the bitwise operators should have been
> | there in the first place - functions can do the same things just as
> | well.
>
> You _ARE_ joking about this, aren't you?
I'm half-serious - the language would not lose too much without
operators for those operations. I'm absolutely serious that I don't
want to see any further operators added for such artificial special
cases.
> Let's say I want to work with a 64-bit number:
What is a 64-bit number in Python?
> >>> b = -a
> >>> print hex(b >> 1)
> -0x4000000000000000L
>
> # !!! SURPRISE !!!
> # (I expected -0xC000000000000000L !!!)
Can you explain why you expected this? How could Python possibly know
how many bits you expected to be in the internal representation?
> I think this should work the same way an 'int' does-- but I
> don't know how to do that without breaking older code...
Add a function
def rightshift(number, shiftwidth, numberwidth=32, expandsign=1)
That function will work for 32-bit numbers, 64-bit numbers, and
97-bit-numbers. No need for a new operator.
Regards,
Martin
More information about the Python-list
mailing list