unsigned integers

Giovanni Bajo noway at sorry.com
Thu Mar 6 09:35:13 EST 2003


Hello,

I have some troubles with signed/unsigned integers, probably because it is
still not clear to me how Python works with respect to this. Especially:

1) How can I force 0xFFFFFFFF to be 4294967295 instead of -1? My problem is
that I'm converting some C code like a = b*c where the result needs to be
wrapped within 32-bit limits. If I bit-and the result with 4294967295
everything is ok, but if I use 0xFFFFFFFF the number does not mask
correctly, because 0xFFFFFFFF is seen as -1. Why should it ever consider a
hex literal as negative, by the way? Is there any real-world case where this
is needed?

2) binascii.crc32() returns a signed integer representing the CRC. I call it
signed because if I print the result it displays a signed number. Now, what
if I need the unsigned representation of it (as in, the unsigned number
which is machine-represented with the same 32 bits)? I need to multiply it
by another integer, but I need an unsigned multiplication, not a signed one.

Thanks
Giovanni






More information about the Python-list mailing list