Sum of Two Integers
Sum of Two Integers is LeetCode 371 (Medium). Given two integers a and b, return their sum without using the + or - operators.
- Both numbers are between -1,000 and 1,000.
- Either can be negative, so the method has to work on two's complement bit patterns, not only on positive numbers.
- -1000 <= a, b <= 1000
Intuition
Binary addition works column by column, like adding on paper. In each column:
- The sum bit is 1 when exactly one of the two bits is 1. Across all columns at once that is
a ^ b. - A carry appears when both bits are 1, and it belongs one column to the left. That is
(a & b) << 1.
So a + b equals (a ^ b) + ((a & b) << 1). That is still an addition, but every carry has moved one column left. Repeat with the two new numbers until the carry is 0; then the sum bits alone are the answer.
Whenever an interview bans + and -, the answer is this XOR-and-carry loop. Divide Two Integers rebuilds division from shifts in the same spirit.
Approach
Before reading on, add 0111 and 0001 by hand and note which columns produce a carry. Then write that rule with ^, & and <<. Finally, decide what Python needs, since its integers never overflow.
Split one addition into two parts
a ^ b adds every column but drops the carries. (a & b) << 1 collects exactly those dropped carries, each moved one column left. Together the two numbers still add up to a + b.
Repeat until nothing is carried
Set a to the sum bits and b to the carry, and repeat while b is not 0. Compute the carry before overwriting a, because the carry needs the old a.
Keep Python to 32 bits
Python integers have no fixed width, so mask a, b and each carry with 0xFFFFFFFF. A negative number then looks like its 32-bit two's complement pattern, and a carry out of bit 31 falls off as it would in hardware.
Turn the pattern back into a signed number
If the result is 0x80000000 or more, its top bit is set and it stands for a negative number. ~(a ^ mask) flips the low 32 bits and then the whole number, which gives that negative value. C++ and Java need neither step.
Why the loop always ends
The new carry comes from bits shared with the old carry, shifted one more place left, so the carry's lowest 1 bit moves up at least one place every round. After at most 32 rounds it has left all 32 bits and the carry is 0.
Code in Python, C++ and Java
& mask keeps exactly these 32 bits, which matters once negatives appear.& mask keeps exactly these 32 bits, which matters once negatives appear.& mask keeps exactly these 32 bits.~(a ^ mask) to get -3. C++ and Java read the bits as -3 directly.Common pitfalls
Leaving out the mask in Python
while b:
a, b = a ^ b, (a & b) << 1while b:
carry = ((a & b) << 1) & mask
a = a ^ b
b = carryPython's -1 has endless 1 bits, so on a = -1, b = 1 the carry moves left forever and never becomes 0. The mask cuts it off at bit 31.
Returning the masked pattern as it is
return a
return a if a < 0x80000000 else ~(a ^ mask)
After masking, every result is between 0 and 2^32 - 1. On a = -1, b = -2 this returns 4294967293 instead of -3.
Shifting a negative int left in C++
int carry = (a & b) << 1;
unsigned carry = (unsigned)(a & b) << 1;
For a = -1, b = -2, a & b is negative, and shifting a negative int left is undefined before C++20. LeetCode's sanitizer stops with "left shift of negative value"; shifting an unsigned is always defined.
Edge cases
The carry ripples through all 32 bits, one place per round, and drops off the top, so the loop runs 32 times before returning 0. No input takes more rounds.
The loop does not run at all and a comes back unchanged.
Complexity
Sum of Two Integers FAQ
How do you subtract without the minus operator?
Negate b with two's complement, ~b + 1, and add: getSum(a, getSum(~b, 1)). The same carry loop then does the subtraction, including the borrow.
Can the bitwise addition be written recursively?
Yes: return a when b is 0, otherwise return getSum(a ^ b, (a & b) << 1). It runs the same rounds, at most 32 calls deep, and needs the same masking in Python.