Skip to content

Coding patternsBlind 75

Sum of Two Integers

Add two numbers without + or −. Solved two ways, a bit-by-bit adder that always runs 32 times and a carry loop that stops when no carry is left, with a step-through, code in five languages and the Big O worked out from the code.

You are given two whole numbers first and second. Return first + second, but without using the + or − operators anywhere in your code.

Example 1

Input: first = 13, second = 11

Output: 24

In binary, 1101 plus 1011 is 11000. The picture shows the low 8 bits of each.

first = 13
00001101
second = 11
00001011
sum = 24
00011000

Example 2

Input: first = -5, second = 3

Output: -2

Negative numbers work too, because they are stored as 32-bit two’s complement.

Example 3

Input: first = 0, second = 7

Output: 7

Adding 0 changes nothing. The carry loop stops after one run.

  • −1,000 ≤ first, second ≤ 1,000
  • Below, b also stands for the number of bits (32) inside O(b).
Can a or b be negative?
Yes. Numbers are 32-bit two's complement, and the same bit rules add them. Python has no fixed width, so it needs a mask.
Can we use sum(), or add 1 at a time with ++?
No. The sum itself must come from bit operators: &, |, ^ and shifts. A loop counter is fine.
Can the result overflow?
No. Both numbers are at most 1,000 in size, so the sum fits easily in 32 bits.

We cannot use +, so we must build addition from bit operators. In one column, first_bit ^ second_bit is the sum bit and first_bit & second_bit is the carry. That is bit manipulation.

This problem has two approaches. Both are O(b) in the worst case, but the carry loop usually stops much sooner.

Carry loop O(b)

Do all 32 columns at once, and handle the carries afterward:

  • first ^ second adds every column but ignores the carries.
  • first & second marks the columns where both bits are 1. Each one makes a carry, and << 1 moves the carries into the next column.

So first + second equals (first ^ second) + ((first & second) << 1). That is still an addition, so repeat the same trick on those two numbers. Stop when the carry is 0, because then first ^ second is the whole sum.

first
00001101
second
00001011
  1. while second != 0:
  2. carry = (first & second) << 1
  3. first = first ^ second
  4. second = carry
  5. return first

first ^ second adds every column but drops the carries. (first & second) << 1 is exactly those carries, moved one column left. So first + second = (first ^ second) + ((first & second) << 1). Repeat until there is no carry.

1 / 10
Solution.javasolution.pySolution.cssolution.gosolution.ts
class Solution {
// Carry loop: first ^ second is the sum without carries, (first & second) << 1 is the carries.
// One run per step of the longest carry chain, at most 32: O(b) time, O(1) memory.
public int getSum(int first, int second) {
while (second != 0) {
int carry = (first & second) << 1;
first = first ^ second;
second = carry;
}
return first;
}
}
Time O(b)Space O(1)Here is how we get these from the code, in four steps.
  1. 1 Find the work that repeats

    The lit lines do one &, one shift and one ^ on whole 32-bit words. That is 1 step. They sit inside one loop.

    1. while second != 0:
    2. carry = (first & second) << 1
    3. first = first ^ second
    4. second = carry
    5. return first
  2. 2 Count the runs on Example 1

    Example 1 adds 13 and 11. The table shows first and the carry second after each run.

    4 runs, against 32 for the bit-by-bit adder.

    run1234
    first after6201624
    second (carry) after18480
    total
    4 runs
  3. 3 Write the count for any n

    Each run moves every carry one column to the left. A carry either dies out or slides off the top after at most b moves:

    at most b runs, and b = 32

    Check it with Example 3: 0 & 7 = 0, so 1 run. The worst case is a carry that ripples through every column, like −1 + 1, which takes 32 runs.

  4. 4 Keep only the part that grows fastest

    There is nothing to drop. The worst case is b, the same as the first approach. But the count follows the longest carry chain, which is short for most numbers.

    So the time is O(b).

  5. S Space: count the extra memory the same way

    The code keeps only carry, besides first and second. The space is O(1).

    carry

2,000 random pairs from −1,000 to 1,000, with signs mixed on purpose. Bit by bit and the carry loop gave the same answer every time, and matched plain addition.

Largest inputsResult
−1 + 1, where the carry runs through all 32 bits, the most loops. Carry loop.Under 0.001 s, and under 0.001 MB of extra memory: just carry, first and second.
All 256 pairs of 16 edge values, such as −1,000, −513, 0, 255 and 1,000. Both approaches.Every sum was right.