Sum of Two Integers
Add two integers without + or -, using XOR for the sum bits and AND for the carries.
The problem
You get two integers a and b, each between -1000 and 1000. Return their sum without using the + or - operators.
Input a = 13, b = 6
Output 19
1101 ^ 0110 = 1011 is the sum without carries, and (1101 & 0110) << 1 = 1000 is the carry. Repeating the same two steps until the carry is 0 gives 10011, which is 19.
Input a = -4, b = 7
Output 3
-4 is stored in two's complement, so the same steps work; the last carry runs off the top of the 32 bits.
Key constraint: Negative numbers are stored in two's complement, so one carry loop handles them too, as long as everything stays within 32 bits.
How to spot the pattern
- The problem bans the arithmetic operators, so only bit operations are left.
- Adding is XOR at every bit plus a carry wherever both bits are 1.
The idea
When you add two bits, the result bit is their XOR, and a carry appears only where both are 1, which is their AND, moved one place left. So a ^ b is the sum without carries and (a & b) << 1 is the carries, and you add those two the same way, again and again, until the carry is 0. The carry moves at least one place left every round, so the loop ends within 32 rounds.
Brute force: Simulate pen-and-paper addition one bit position at a time with a separate carry variable, which also takes 32 steps but needs more code.
Step by step
- 1While
bis not 0, computecarry = (a & b) << 1. - 2Set
a = a ^ b, the sum without carries. - 3Set
b = carryand repeat. - 4When
bis 0, returna. - 5In Python, mask every value to 32 bits, and turn a final result above 0x7FFFFFFF back into a negative number.
Code
class Solution:
def getSum(self, a: int, b: int) -> int:
MASK = 0xFFFFFFFF # Python ints never overflow, so keep 32 bits by hand
a &= MASK
b &= MASK
while b:
carry = ((a & b) << 1) & MASK # where both bits are 1, a carry moves one place left
a ^= b # add each bit without the carry
b = carry
# a now holds the 32-bit pattern of the sum; patterns above 0x7FFFFFFF are negative
return a if a <= 0x7FFFFFFF else ~(a ^ MASK)Complexity
Time
O(1)
Space
O(1)
At most 32 rounds for 32-bit integers, since the carry moves left every round.
Watch out for
- In C++, shifting a negative int left is undefined behavior before C++20, so do the work on unsigned numbers.
- In Python, integers never overflow, so a negative carry grows forever unless you mask every step to 32 bits.
- Forgetting to turn the masked Python result back into a negative number.
Follow-up questions
How would you subtract without -?
Add the negation: in two's complement -b is ~b + 1, and that + 1 can use the same carry loop.
Why does the loop always end?
Every round the carry gains at least one more 0 at the bottom, so within 32 rounds it becomes 0. In Python, the mask is what makes the carry fall off the top.
In Hinglish
XOR bina carry ke jod deta hai aur `(a & b) << 1` carry batata hai. Jab tak carry zero na ho, dono ko isi tarah phir se jodte raho.