Overflow-less integer arithmetic?

Erik Max Francis max at alcyone.com
Mon Aug 20 11:15:53 EDT 2001


Tim Peters wrote:

> You're getting screwed by the hex literal:  you're doing
> 
>     long & int
> 
> so the int part gets converted to long first.  But 0xffffffff is -1 as
> a
> short int (on a 32-bit box), so coerces to -1L, i.e. to an infinite
> string
> of 1 bits.
> 
>     i & 0xffffffff == i
> 
> is thus always true on a 32-bit box, regardless of the value of i, and
> regardless of whether i is short or long.  Stick an L at the end of
> the hex
> literal to get 32 1-bits, which is infinitely fewer than you're
> getting now
> <wink>.

Aha!  Longs are unbounded, so I don't want to and it with -1l, I want to
and it with 1l << 32.  I seem to recall trying to get the effect I
wanted with 0xffffffffl and it didn't seem to work, but I may have just
goofed it.

Thanks!  (For the record, I did want 32-bit signed ints, so preserving
the sign is fine.)

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ But in matters fundamental / We are patterned on an old design
\__/ Oleta Adams
    Rules for Buh / http://www.alcyone.com/max/projects/cards/buh.html
 The official rules to the betting card game, Buh.



More information about the Python-list mailing list