Bit-twiddling

Lars Marius Garshol larsga at ifi.uio.no
Wed Jul 28 17:03:06 EDT 1999


I'm translating a numerical algorithm from C to Python, but have run
into a problem in that the algorithm seems to rely on the fact that
shifting, addition and multiplication all 'wrap' when the result
exceeds 32 bits.

Since the Python equivalents do not, does there exist a common
workaround for this?

For example, I'd like

    mt[0]= seed & 0xffffffff
    for mti in range(1,n):
        mt[mti]=(69069 * mt[mti-1]) & 0xffffffff

to never put anything that doesn't fit in an unsigned long in the mt
list.

Any ideas?

--Lars M.




More information about the Python-list mailing list