150NeetCode 150

149.Sum of Two Integers

MediumBlind 75Bit Manipulation ยท 6/7

Problem

Given two integers a and b, return their sum without using the operators + and -.

This is really a question about how hardware adds numbers: you must build addition out of bitwise operations (^, &, <<), and it must work for negative numbers too.

Examples

Input:  a = 1, b = 2
Output: 3
Input:  a = 2, b = 3
Output: 5
Explanation: 010 + 011 = 101.
Input:  a = -1, b = 1
Output: 0
Explanation: In two's complement, ...1111 + ...0001 carries all the way out of the 32nd bit, leaving 0.

Constraints

  • -1000 <= a, b <= 1000

๐Ÿ’ก Key Insight

Binary addition of two bits splits into two parts: the sum bit without carry is x ^ y, and the carry is x & y, which belongs one position to the left. So a + b == (a ^ b) + ((a & b) << 1). Repeat with the new pair until the carry is 0. Java ints are 32-bit two's complement, so negatives work automatically and any carry past bit 31 simply falls off.

Brute Force (Bit-by-Bit Full Adder)

TimeO(32) = O(1)SpaceO(1).

Intuition

Simulate a ripple-carry adder, one bit position at a time, exactly like a circuit. For bit i with input bits x, y and incoming carry c:

  • sum bit = x ^ y ^ c
  • carry out = (x & y) | (x & c) | (y & c) (at least two of the three are 1)

Do this for all 32 positions and assemble the result.

Algorithm

  1. result = 0, carry = 0.
  2. For i from 0 to 31:
    • x = (a >> i) & 1, y = (b >> i) & 1.
    • sum = x ^ y ^ carry; carry = (x & y) | (x & carry) | (y & carry).
    • result |= sum << i.
  3. Return result.

Visualization

a = 2 = 010, b = 3 = 011

bit 0: x=0 y=1 c=0 โ†’ sum 1, carry 0
bit 1: x=1 y=1 c=0 โ†’ sum 0, carry 1
bit 2: x=0 y=0 c=1 โ†’ sum 1, carry 0
result = 101 = 5

Code

java
class Solution {
    public int getSum(int a, int b) {
        int result = 0;
        int carry = 0;
        for (int i = 0; i < 32; i++) { // loop counter only; no + used on a or b
            int x = (a >> i) & 1;
            int y = (b >> i) & 1;
            int sum = x ^ y ^ carry;                          // sum bit of a full adder
            carry = (x & y) | (x & carry) | (y & carry);      // carry if at least two bits are 1
            result |= sum << i;
        }
        return result; // carry out of bit 31 is discarded (two's-complement wraparound)
    }
}

Complexity

  • Time: O(32) = O(1) โ€” one step per bit.
  • Space: O(1).

Complexity Comparison

Approach Time Space
Bit-by-Bit Full Adder O(32) O(1)
Recursive XOR and Carry O(32) O(32) stack
Iterative XOR and Carry O(32) O(1)

Common Pitfalls

  • Overwriting a before computing the carry โ€” compute (a & b) << 1 from the old a, then update a.
  • Porting this to Python: integers are unbounded, so for negatives the carry never falls off and the loop runs forever. You must mask with 0xFFFFFFFF and convert back; Java's fixed 32-bit int does this for free.
  • Thinking negatives need special handling โ€” two's complement makes subtraction the same circuit as addition.
  • Using >> versus << incorrectly: carries move left (towards higher bits), so it is always << 1.