Weird 'and' result...

Erik Max Francis max at alcyone.com
Sat Jun 21 02:09:30 CEST 2003


Peter Hansen wrote:

> I don't know the details, since the first value won't fit
> in an int, it's quietly making it a long, then treating the
> second value as "-1" and/or basically sign-extending it
> to an appropriate size to do the AND for you...
> 
> Change it to 0xffffffffL (note the L) and it works "better".
> 
> A guru will be along shortly to explain the details. :-)

Yes, that's exactly it.  Since int is signed 32 bits, 0xffffffff is a
signed -1.  Since -1 is all bits one, x & -1 == x.  If you really want
the unsigned value 0xffffffff, you need to use a long like you
suggested.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ A good indignation brings out all one's powers.
\__/  Ralph Waldo Emerson




More information about the Python-list mailing list