[Python-Dev] Negative long literals (was Re: Does Python need a '>>>' operator?)

Michael Gilfix mgilfix@eecs.tufts.edu
Mon, 10 Jun 2002 10:00:33 -0400


On Mon, Jun 10 @ 09:41, Tim Peters wrote:
> If you have a *positive* long that would fit in a 32-bit unsigned int (which
> type Python doesn't have), and know you're running on a 32-bit box, and only
> want the same bit pattern in an int, you can do
> 
> def toint32(long):
>     if long & 0x80000000L:
>         long -= 1L << 32
>     return int(long)
> 
> This also raises OverflowError if you're mistaken about it fitting in 32
> bits.

  Whoops. That should have been a 0x8... in my example :)

   At ny rate, I wish this function was available as a built-in. It
would be nice if I had some conversion function where python looked
at the highest bit and treated that as my word boundary to determine
whether the number is positive or not.

                           -- Mike

-- 
Michael Gilfix
mgilfix@eecs.tufts.edu

For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html