AlgoMaster Logo

Sum of Two Integers

Last Updated: March 20, 2026

medium
3 min read

Understanding the Problem

At first glance, this seems impossible. How do you add two numbers without addition or subtraction? The answer lies in how computers actually perform addition at the hardware level: using bitwise operations.

When you add two binary digits, two things happen. You get a sum bit (which is XOR) and a carry bit (which is AND, shifted left). A CPU's adder circuit does exactly this, repeatedly, until there's no carry left. Our job is to replicate that logic in code.

The real insight here is that addition is fundamentally a bitwise operation. The + operator is just a convenient abstraction over XOR and AND.

Key Constraints:

  • -1000 <= a, b <= 1000 → The values are small, so even an O(n) approach where n = |a| or |b| would work. But we should aim for O(1) since this is a bit manipulation problem.
  • The problem explicitly bans + and - → We must use bitwise operators (AND, OR, XOR, shift, NOT).

Approach 1: Successive Increment/Decrement

Intuition

The simplest way to think about this: if you can't add directly, just count up (or down) one at a time. To compute a + b, start at a and move b steps in the right direction. Each step uses bitwise tricks to increment or decrement by 1 without using + or -.

How do you increment by 1 without +? Think about what happens in binary when you add 1. You flip the lowest 0-bit to 1 and clear everything below it. The expression -~x achieves this (negating the bitwise complement), which is equivalent to x + 1 without using +. Similarly, ~-x gives x - 1.

This isn't a practical solution, but it establishes a baseline and shows we can manipulate numbers without arithmetic operators.

Algorithm

  1. If b is positive, increment a by 1 exactly b times (using -~a to increment).
  2. If b is negative, decrement a by 1 exactly |b| times (using ~-a to decrement).
  3. If b is zero, return a directly.
  4. Return the final value of a.

Example Walkthrough

1Start: a=1, b=2. We need to increment a by 1, b times
0
a=1
1
1
2
b=2
1/4

Code

This approach works but is painfully slow for large values of b. We're processing one unit at a time instead of handling all bit positions simultaneously. What if we could process all 32 bits in parallel, the way a real CPU adder does?

Approach 2: Bit Manipulation (XOR + AND + Shift)

Intuition

Let's go back to basics. How does binary addition actually work?

When you add two single bits:

  • 0 + 0 = 0 (no sum, no carry)
  • 0 + 1 = 1 (sum = 1, no carry)
  • 1 + 0 = 1 (sum = 1, no carry)
  • 1 + 1 = 10 (sum = 0, carry = 1)

Look at the "sum" column: it's exactly XOR. And the "carry" column: it's exactly AND. The carry needs to be added to the next higher bit position, so we shift it left by 1.

So for two numbers a and b:

  • a XOR b gives us the sum of all bit positions where there's no carry.
  • (a AND b) << 1 gives us all the carries, shifted to their correct positions.

But wait, we still need to "add" the partial sum and the carry together. We can't use +, but we can apply the same XOR/AND trick again. And again. And again, until the carry becomes zero.

This is exactly how a ripple-carry adder works in hardware. Each iteration propagates carries one position further. Since integers are 32 bits wide, we'll never need more than 32 iterations.

Algorithm

  1. While b (the carry) is not zero:
    • Compute the carry: carry = (a AND b) << 1
    • Compute the partial sum without carry: a = a XOR b
    • Set b = carry for the next iteration
  2. When b becomes 0, all carries have been resolved. Return a.

Example Walkthrough

1Start: a=13 (1101), b=10 (1010). Compute XOR and AND.
0
a=1101
1
1
1
2
0
3
1
4
b=1010
1
5
0
6
1
7
0
1/6

Code