LeetCode #371 Medium

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.
Constraints
  • -1000 <= a, b <= 1000
bit-manipulation
Open on LeetCode ↗
02

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.

How to spot this pattern

Whenever an interview bans + and -, the answer is this XOR-and-carry loop. Divide Two Integers rebuilds division from shifts in the same spirit.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

5

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def getSum(self, a: int, b: int) -> int:
▶3 mask = xFFFFFFFF
▶4 a, b = a & mask, b & mask
▶5 while b:
▶6 carry = ((a & b) << 1) & mask
▶7 a = a ^ b
▶8 b = carry
▶9 return a if a < x80000000 else ~(a ^ mask)
bits 31–876543210a0…0000000102b0…0000000113carrysum bitskeep both numbers as 32-bit patterns
a2
b3
Each number is drawn as its 32 bits; the left tile stands for bits 31 to 8, which are all equal here. In Python, & mask keeps exactly these 32 bits, which matters once negatives appear.
bits 31–876543210a0…0000000102b0…0000000113carry0…0000001004sum bitsround 1: both 1 at bit 1 → carry moves left
a & b2columns with two 1s
carry4
A column overflows only when both bits are 1, which happens at bit 1. Each overflow adds 1 to the next column on the left, so the carry is those columns shifted left by one.
bits 31–876543210a0…0000000102b0…0000000113carry0…0000001004sum bits0…0000000011sum bits: 1 where exactly one of a and b is 1
a ^ b1
next a1
next b4
Add each column on its own and drop the overflow: 1 + 0 gives 1, 1 + 1 gives 0. That is exactly XOR. The carries still have to be added, so the next round adds 1 and 4.
bits 31–876543210a0…0000000011b0…0000001004carry0…0000000000sum bitsround 2: no column has two 1s → carry 0
a & b0columns with two 1s
carry0
No column has two 1s, so nothing overflows and the carry is 0. The sum bits below will be the final answer.
bits 31–876543210a0…0000000011b0…0000001004carry0…0000000000sum bits0…0000001015sum bits: 1 where exactly one of a and b is 1
a ^ b5
next a5
next b0
Add each column on its own and drop the overflow: 1 + 0 gives 1, 1 + 1 gives 0. That is exactly XOR. The carry is 0, so this is the full sum.
bits 31–876543210a0…0000001015b0…0000000000carrysum bitscarry is 0 → answer: 5
b0loop ends
return5
Nothing is left to carry, so a holds the sum after 2 rounds. Its top bit is 0, so the pattern is the answer as it stands: 5.
05

Common pitfalls

Leaving out the mask in Python

✗ Wrong
while b:
    a, b = a ^ b, (a & b) << 1
✓ Right
while b:
    carry = ((a & b) << 1) & mask
    a = a ^ b
    b = carry

Python'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

✗ Wrong
return a
✓ Right
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++

✗ Wrong
int carry = (a & b) << 1;
✓ Right
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.

06

Edge cases

a = -1, b = 1

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.

b = 0

The loop does not run at all and a comes back unchanged.

07

Complexity

Time
O(1)
Space
O(1)
At most 32 rounds for 32-bit integers, since every round pushes the carry's lowest 1 bit at least one place left. For w-bit integers that is O(w) rounds of constant work.
08

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.