Sum of Two Integers asks you to add two numbers without using the + or - operator. It is a classic bit-manipulation puzzle: it forces you to rebuild addition from the gates a CPU actually uses — XOR and AND.
Problem. Given two integers a and b, return their sum, but you may not use the + or -
operators.
Example: a = 2, b = 3 → 5. In binary, 0010 + 0011 = 0101.
The slow way first
The tempting "cheat" is sum(range...) or repeatedly incrementing one number b times — but incrementing is just + 1 in disguise, and looping b times is O(b), which is hopeless for large or negative numbers. We need to add in one shot, the way hardware does.
The question to ask: how does a circuit add two bits without already knowing how to add? It splits the job into two pieces — the sum bit and the carry bit — each of which is a single logic gate.
The idea: XOR adds, AND carries
Look at adding two single bits:
0 + 0 = 0,0 + 1 = 1,1 + 0 = 1,1 + 1 = 10(sum 0, carry 1).
That sum-without-carry column is exactly XOR (a ^ b). The carry happens only when both bits are 1, which is exactly AND (a & b), and a carry belongs in the next column to the left, so we shift it: (a & b) << 1.
So one pass gives us a partial sum (a ^ b) plus a set of carries ((a & b) << 1). We just repeat: add the carries in the same way, until there are no carries left.
The whole loop is: while b: carry = (a & b) << 1; a = a ^ b; b = carry. When b (the carry) finally becomes 0, a holds the answer.
Walk through it
Step through the animation with a = 2 (0010) and b = 3 (0011). Each loop highlights the columns where both bits are 1 (those generate a carry), XORs the rows into the new a, then feeds the shifted carry back into b. After two loops the carry drains to 0 and a reads 0101 = 5.
Pseudocode
while b is not zero:
carry = (a AND b) shifted left by 1 # columns where both are 1 carry into the next
a = a XOR b # add the bits, ignoring carry
b = carry # the carries become the next thing to add
return a # no carry left -> a is the sumThe Python solution
def get_sum(a, b):
# a, b are 4-bit here for the picture
while b:
carry = (a & b) << 1
a = a ^ b
b = carry
return awhile b:keeps looping as long as there is still a carry to add.carry = (a & b) << 1finds every column where both numbers have a 1 and moves that carry one place left.a = a ^ badds the two numbers column-by-column without carrying.b = carrymakes the leftover carries the new thing to add, then we go around again.- When
breaches 0 there is nothing left to carry, soais the final sum.
In real Python, integers are arbitrary-precision, so a negative b never lets this loop terminate. The
usual fix is to mask with 0xFFFFFFFF to emulate 32-bit wraparound and then convert back to a signed
value. The 4-bit positive example here keeps the idea clear without that machinery.
Complexity
| Case | Time | Notes |
|---|---|---|
| Increment b times (cheat) | O(b) (moderate) | loops once per unit |
| Bitwise XOR + carry | O(1) (fast) | fixed-width: at most ~32 carry passes |
O(1) (fast)For fixed-width integers the carry can ripple at most as many times as there are bits, so the loop runs a constant number of times — effectively O(1) with O(1) extra space.
When this pattern shows up
Whenever a problem bans an arithmetic operator or asks you to think in bits, remember the two workhorse gates: XOR is addition without carry, and AND marks where a carry happens. The same split powers "missing number," "single number," and adder-style puzzles.
Do not forget the shift on the carry. (a & b) alone places the carry in the wrong column — it must move
one position left with << 1 to land in the next column up.
Practice
Adding a = 2 (0010) and b = 3 (0011): what is the very first carry, (a & b) << 1?
1. Which operation gives the sum of two bits ignoring the carry?
2. Why do we shift the AND result left by 1?
3. When does the loop stop?
4. What is the extra space used by this solution?