Skip to content
AI360Xpert
Beta
Difficulty: MediumBit Manipulation

Sum of Two Integers

Problem in Plain English

Compute a + b using only bitwise operators, never + or -. The method mirrors how hardware adders combine a carry-free sum with a shifted carry until nothing carries.

Problem Statement

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

Think like a hardware adder: one bitwise pass adds every position with no carry, a second pass finds where carries arise, and the two results are combined until no carry remains.

Constraints

  • −1000≤a≤1000-1000 \le a \le 1000
  • −1000≤b≤1000-1000 \le b \le 1000
  • Neither + nor - may appear in the solution logic.

Examples

Example 1

Input
a = 1, b = 2
Output
3

Plain addition gives 1 + 2 = 3, rebuilt here with bitwise steps.

Example 2

Input
a = 2, b = 3
Output
5

The bits of 2 and 3 combine with one carry round to reach 5.

Example 3

Input
a = -2, b = 3
Output
1

Mixed signs flow through the same carry loop to give -2 + 3 = 1.

Example 4

Input
a = -1, b = 1
Output
0

Opposite values cancel. In a 32-bit carry loop, the carry travels through the run of 1 bits and then falls outside the word; the remaining word is zero.

Example 5

Input
a = -1000, b = 0
Output
-1000

A zero second operand requires no carry rounds or unit transfers. Python must still return the signed value when decoding the masked initial word.

Intuition

At one bit position, two different bits produce a sum bit of 1 and no carry; two 1 bits produce a sum bit of 0 and a carry into the next position. XOR gives all carry-free sum bits at once, while AND followed by a left shift places all carries into their destination positions. For a = 1 and b = 2, no positions overlap, so XOR immediately gives 3 and the carry is zero. Negative inputs require the same finite 32-bit representation throughout.

Approaches

Unit Step Loop

Solution Details

Reveal Unit Step Loop: intuition, complexity, and code

Hints

Hint 1
As a baseline, transfer one unit from b toward a while preserving their total. Implement each increment by flipping binary bits rather than using a forbidden arithmetic operator.
Hint 2
Notice that a ^ b adds every bit position while ignoring carries.
Hint 3
(a & b) << 1 marks exactly where carries appear, so repeating xor-then-carry until the carry is zero finishes the addition.

Edge Cases

  • When b is zero, the loop can stop immediately; when only a is zero, the loop still needs to incorporate b. Python must decode an initially masked negative a even when b is zero.
  • Negative inputs such as a = -2, b = 3 must flow through the same carry loop and yield 1.
  • In Python, unbounded integers need a 0xFFFFFFFF mask or the carry loop never terminates on negatives.
  • A carry chain such as 511 and 1 propagates across many columns; opposite signs may use all 32 rounds despite small magnitudes.
  • Both negative operands, including -1000 and -1000, must decode to a signed negative result, -2000.

Common Mistakes and Interview Tips

  • Forgetting to shift the carry left, which adds it back into the same position it came from.
  • Skipping the 32-bit mask in Python, so negative carries loop forever.
  • Testing only positives and missing that a = -1, b = 1 must give 0.
  • Writing ++, --, += or -= in the baseline: the operator restriction applies to both approaches and Python has no increment operator.
  • Computing the carry after overwriting a, so it no longer reflects the same old operands used for XOR.
  • Returning the masked Python word directly for a negative result, which yields a large positive integer instead.

Key Takeaway

Xor adds without carry while and-plus-shift computes the carry: looping those two steps simulates the adder circuit inside every processor.