bit shifting question

Scott David Daniels Scott.Daniels at Acm.Org
Mon May 17 17:02:50 EDT 2004


Grant Edwards wrote:

> On 2004-05-17, Michael Hudson <mwh at python.net> wrote:
> 
>>>My question is does python share java's peculiar mode of bit
>>>shifting, or does python adhere closer to c's bit shifting?
>>
>>The right shift in Python is arithmetic, i.e. preserves the
>>sign bit.
> 
> Which one is the sign bit?  IOW, how wide is a "word"?
In most cases, you should not be able to discover how wide
a "word" is.  Python is trying to move to a single integer
data type which is a combination of the current "long" and
"int" where the internal representation is an implementation
detail.  Unfortunately, shifting is one of those places you
can still discover the "width"  with code like:

     def wordwidth():
         for shift in range(256):
             if 1 << shift < 0:
                 return 1 + shift

-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list