integer type conversion problem/question

Terry Reedy tjreedy at udel.edu
Sat Oct 9 12:39:50 EDT 2004


>Since in theory Python ints are  larger than C ints
> (since Python ints are implemented as C long ints),

Wrong premise.  On many (most?) systems today, C long == C int == 32 bits.

> there should be (in theory) no problem converting C unsigned ints to
> Python integers (corresponding to C long integers)

Hence wrong conclusion, as you discovered.  Unsigned C int > 2**31 does not 
convert properly to C long when C long == C int.

If you must program your own RNG, simplest solution is to limit it to range 
[0, 2**31-1] so signed/unsigned does not matter.

Terry J. Reedy






More information about the Python-list mailing list