Search code examples
pythoninteger64-bitaddition

Truncate integers when more than 64 bits


I'm trying to perform some 64 bit additions, ie:

a = 0x15151515
b = 0xFFFFFFFF
c = a + b

print hex(c)

My problem is that the above outputs:

0x115151514

I would like the addition to be 64 bit and disregard the overflow, ie expected output would be:

0x15151514

NB: I'm not looking to truncate the string output, I would like c = 0x15151514. I'm trying to simulator some 64 bit register operations.


Solution

  • Then just use the logical and operator &

    c = 0xFFFFFFFF & (a+b)
    

    By the way, these are 32 bit values, not 64 bit values (count the F; every two F is one byte == 8 bit; it's eight F, so four byte, so 32 bit).