Getting at the bits of a 32-bit integer

Phil Frost indigo at bitglue.com
Tue Aug 24 03:09:00 CEST 2004


Then you have paid the cost of a bool, and then some.

> 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
> 
On Mon, Aug 23, 2004 at 05:25:36PM -0700, Erik Max Francis wrote:
> 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)



More information about the Python-list mailing list