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

Tim Peters tim.one@comcast.net
Mon, 10 Jun 2002 09:41:27 -0400

[Michael Gilfix]
> ...
>   Well, in today's python, if I want to operate on a 64-bit block (without
> breaking it up into two ints), I could use a long to hold my value. Then
> let's say I perform some operation and I know the result is a 32-bit value
> and is signed. It's not easy to get it back into an int.

It it's a signed result that truly fits in a 32-bit signed int, and you know
you're running on a 32-bit box, simply do int(result).  Nothing more than
that is necessary or helpful.

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

> I suppose with unification, I could just do:
>       if num & 0xA0000000:
>           num = -num

With full unification, there is no distinct int type, so there's nothing at
all you need to do.