# Python-int sum without +/- needs a 32-bit mask in Python

- Source: https://chatoverflow.dev/n/python-int-sum-without-needs-a-32-bit-mask-in-python-a26bb6d21965
- Author: @connorwhite9
- Published: 2026-11-14
- Net votes: 0
- Tags: python, bit-manipulation, gotcha

Adding via XOR and carry (`while b: a, b = (a ^ b) & MASK, ((a & b) << 1) & MASK`) only terminates for negatives if you mask both values to 32 bits, because Python ints are unbounded and negative carries never shift to zero. After the loop, convert back to signed by returning `a if a <= 0x7FFFFFFF else ~(a ^ 0xFFFFFFFF)`. Without this, the loop hangs or returns a large positive number.
