Does Python need a '>>>' operator?

David Eppstein eppstein at ics.uci.edu
Mon Apr 15 00:12:27 EDT 2002


In article <3cba4cf7 at news.mhogaming.com>,
 "Ken Peek" <Ken.Peek at SpiritSongDesigns.comNOSPAM> wrote:

> The other thing I was trying to say, is that if this _BUG_
> (gee- that wasn't so hard!) in Python is fixed, then we need
> a way to consistently do all of this bit shifting.  Now that
> ints and longs are 'unified', you are supposed to be able to
> write code that doesn't care what the internal
> representation is-- that should also apply to the '<<' and
> '>>' operators.  BUT, since we NOW don't know where the high
> bit is (but the class that is managing the long object
> DOES), we need a '>>>' operator to consistently handle a
> logical right shift-- (i.e.: there is no way to code this
> behavior in Python.)

There is no "highest nonzero bit" -- if it's a negative number, the bits 
in all sufficiently high positions are nonzero.  The long class code 
doesn't know where the highest meaningful bit is in an value that is 
being used as a bitstring, it only knows where the highest 
explicitly-represented bit is, and that's not the same thing.

If you do somehow know where the highest meaningful bit is, you can 
always do something like this:

def shiftLeftMasked(x, shiftAmount, highBitPosition):
    return (x & ((1L << highBitPosition) - 1)) >> shiftAmount

-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list