Getting at the bits of a 32-bit integer

Erik Max Francis max at alcyone.com
Mon Aug 23 20:25:36 EDT 2004


Phil Frost wrote:

> However, doing this sort of thing is rather nonpythonic. Python
> abstracts the platform's representation of an integer. Integers in
> Python arn't always 32 bits; they are actually the size of C's 'long'
> type, which is to say you can find the size by looking at sys.maxint,
> but it's more trouble than it's worth. You will also encounter
> problems
> if you change the highest bit:
> 
> >>> 10 ^ (1 << 31)
> __main__:1: FutureWarning: x<<y losing bits or changing sign will return
> a long in Python 2.4 and up
> -2147483638
> 
> Note that the error message isn't even accurate. This is probably a
> good
> indication that this isn't a common use case!
> 
> So, short story: use a bool (True/False)

So use a Python long instead!  That way the size of the Python int is
irrelevant.

-- 
 __ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/  \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ It should therefore be difficult in a republic to declare war; but
    not to make peace. -- John Story



More information about the Python-list mailing list