LeetCode 371 · Medium

Sum of Two Integers Explained (LeetCode 371)

Updated Nov 27, 2026 · 3 min read

XOR the two numbers to add every bit column while ignoring carries, and AND them then shift left by one to get the carries. Replace a with the XOR and b with the carry, and repeat until the carry is zero. In Python, mask every step to 32 bits so negative numbers stop, which makes it O(1) time and space.

Watch onYouTubeInstagram

Key points

  • ●XOR gives the sum of each bit column without the carry.
  • ●AND finds columns where both bits are 1; shifting left moves the carry one spot over.
  • ●Repeat until the carry is zero: 5 + 7 takes three rounds.
  • ●Python integers don't stop at 32 bits, so mask with 0xFFFFFFFF or negatives loop forever.
Step-by-step visual sheet: Sum of Two Integers
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: a calculator with no plus key

Maya's calculator lost its plus key. She needs 5 + 7.

That's LeetCode 371, Sum of Two Integers: you get two integers, a and b, each between -1000 and 1000, and you return their sum without using the + or - operators.

The slow way: count up one at a time

Without a plus key, Maya could count up: start at 5, add one, again and again. That is 7 taps for 5 + 7, and a thousand taps for a thousand. Worse, "add one" is still addition. It doesn't really solve the problem. You need a way to add that never uses arithmetic at all, only bit operations like XOR, AND and shifts.

The idea: add like in school, in two passes

Write both numbers in binary: 5 is 0101 and 7 is 0111. Then do school addition, but split it into two passes:

  • XOR adds each column and ignores the carry. A column gives 1 when exactly one of the bits is 1.
  • AND finds the columns where both bits are 1. Those are the carries. Shift that left by one, because a carry goes into the next column.

So a + b is always (a XOR b) + ((a AND b) << 1). That still has a plus in it, but now you can repeat the same trick on the two new numbers. Each round, the carry moves left, and eventually it runs out.

Trace it on 5 and 7

Round a b a XOR b (a AND b) << 1
1 5 7 2 10
2 2 10 8 4
3 8 4 12 0

The carry hits 0 after three rounds, so the answer is 12. Three rounds, not seven taps. Step 4 of the visual sheet above shows the same pairs: (5, 7), (2, 10), (8, 4), (12, 0).

The code in Python

def get_sum(a: int, b: int) -> int:
    MASK = 0xFFFFFFFF
    MAX_INT = 0x7FFFFFFF
    while b != 0:
        partial_sum = (a ^ b) & MASK
        carry = ((a & b) << 1) & MASK
        a, b = partial_sum, carry
    if a <= MAX_INT:
        return a
    return ~(a ^ MASK)

The loop is three lines: XOR for the sum, AND and shift for the carry, swap them in. Work out both values from the old a and b before you assign them, which the tuple assignment on the last loop line does for you. The mask and the last two lines are only there for negative numbers.

Complexity

  • Time: O(1). With 32-bit integers the carry can move left at most 32 times.
  • Space: O(1), a few variables.

Counting up to 1,000 takes a thousand steps. Bits take 32 rounds at most.

The trap: Python and negative numbers

In a language with fixed 32-bit integers, the carry falls off the end. Python integers have no fixed size, so a negative number has an endless row of 1 bits on the left and the carry never runs out. Try -1 and 1 without the mask: the pairs go (-2, 2), (-4, 4), (-8, 8), and the loop never ends.

The fix is step 5 of the sheet. & MASK keeps only the low 32 bits after every step, which acts like a 32-bit machine. At the end, if the result is above MAX_INT (0x7FFFFFFF), its top bit is set, meaning it's a negative 32-bit number, and ~(a ^ MASK) turns it back into a normal negative Python integer. With the mask, -1 + 1 returns 0 and -3 + -5 returns -8.

Frequently asked questions

How do you add two numbers without the + operator?

Split addition into two parts. a XOR b adds each bit column and drops the carry, and (a AND b) shifted left by one is the carry. Feed those two back in as the new a and b until the carry is zero.

Why do you need a 32-bit mask in Python for Sum of Two Integers?

Python integers are not limited to 32 bits, so with a negative number the carry keeps moving left forever. With -1 and 1, the pairs go (-2, 2), (-4, 4), (-8, 8) and never stop. Masking with 0xFFFFFFFF fakes 32-bit integers, and the last step turns the pattern back into a negative number.

What is the time complexity of Sum of Two Integers?

O(1) time and O(1) space for 32-bit integers. The carry moves at least one bit left each round, so the loop runs at most 32 times.

Why does the loop stop when the carry is zero?

Because a always holds the sum so far and b holds what still has to be added. When b is zero there is nothing left to add, so a is the answer.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free