Sum of Two Integers - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

  • Language note (Python vs fixed-width ints):
    This explanation assumes a fixed-width, two’s-complement integer model (as in Java, C/C++, or JavaScript). In Python, integers have arbitrary precision and do not wrap to a fixed number of bits, so this exact implementation will not behave correctly for negative numbers in Python as-is.
  • We are given two signed integers a and b, and we want to compute their sum without using + or -. Instead, we simulate how binary addition works at the bit level.
  • Intuition — Break the Addition into Two Independent Pieces:
    In binary addition, each bit-position produces:
    • a sum bit (the current column’s result)
    • a carry bit into the next higher position
    Instead of directly adding a and b, we form two new numbers whose sum is exactly equal to the intended total:
    • sumWithoutCarries:
      This is the result you would get if you ignored all carries. The truth table per bit-position is:
      • 0 + 0 → 0
      • 0 + 1 → 1
      • 1 + 0 → 1
      • 1 + 1 → 0 (carry is ignored here)
      This behavior matches XOR, so a ^ b is the sumWithoutCarries.
    • missingCarryContribution:
      Carry occurs only when both bits are 1:
      • 1 + 1 is the only case that produces carry (1).
      Bitwise AND finds these positions: a & b. Since the carry belongs to the next higher bit, we shift it left: (a & b) << 1.
    These two numbers,
    • sumWithoutCarries = a ^ b
    • missingCarryContribution = (a & b) << 1
    satisfy the identity:
    sumWithoutCarries + missingCarryContribution = a + b So we can replace a and b with these values and repeat.
  • Iterative Process — Resolve All Remaining Carries:
    After each decomposition:
    • a holds the sumWithoutCarries.
    • b holds the missingCarryContribution that still needs to be added.
    In each iteration:
    • a = a ^ b — new sumWithoutCarries
    • b = (a & b) << 1 — new missingCarryContribution
    The “missing carry” bits keep moving left and getting re-applied until no bit-position produces a carry anymore.

    When b finally becomes 0:
    • No more carry bits remain.
    • Adding 0 to a leaves it unchanged.
    • Thus, a now holds the final answer.
  • Why the loop always terminates (finite number of iterations):
    On every iteration, b becomes the new missingCarryContribution: (a & b) << 1. This implies:
    • Carry bits always shift left toward higher bit-positions.
    • Some carry bits may also disappear early whenever the next iteration’s a has a 0 in that bit-position (because a & b clears such bits).
    A carry cannot persist indefinitely — it can only:
    • keep shifting left until it moves beyond the most significant bit, or
    • be removed earlier when ANDing produces 0 at that position.
    Once all carry bits have either been absorbed or shifted out of range, b becomes 0, and the loop terminates.

Time Complexity

O(1) The loop can run only a bounded number of times because carry bits can shift left at most through a fixed number of bit-positions.

Space Complexity

O(1) Only a few integer variables are used.

Visucodize editorial solution titled JS Solution for LeetCode's Sum of Two Integers coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/sum-of-two-integers.