Given two integers a and b, return the sum of the two integers without using the operators + and -.
Input: a = 1, b = 2
Output: 3
Input: a = 2, b = 3
Output: 5
Input: a = 5, b = 7
Output: 12
-1000 <= a, b <= 1000This is a classic bit manipulation problem that simulates binary addition.
How do we add in binary?
0101 (5)
+ 0111 (7)
------
1100 (12)
For each bit position:
0 + 0 = 0, no carry0 + 1 = 1, no carry1 + 0 = 1, no carry1 + 1 = 0, carry 1Observation:
^) gives us the sum without carry&) finds positions where carry occurs{'<'}{'<'} 1) moves carry to the next positionwhile (b != 0) {
sum = a ^ b // XOR gives sum without carry
carry = (a & b) << 1 // AND finds carry, shift left
a = sum
b = carry
}
return a
Why this works?
b (carry) becomes 0Iteration 1:
a = 5 = 0101
b = 7 = 0111
XOR: 0101 ^ 0111 = 0010 (2) - sum without carry
AND: 0101 & 0111 = 0101 (5) - positions with carry
Carry: 0101 << 1 = 1010 (10) - shift carry left
Now: a = 2, b = 10
Iteration 2:
a = 2 = 0010
b = 10 = 1010
XOR: 0010 ^ 1010 = 1000 (8)
AND: 0010 & 1010 = 0010 (2)
Carry: 0010 << 1 = 0100 (4)
Now: a = 8, b = 4
Iteration 3:
a = 8 = 1000
b = 4 = 0100
XOR: 1000 ^ 0100 = 1100 (12)
AND: 1000 & 0100 = 0000 (0)
Carry: 0000 << 1 = 0000 (0)
Now: a = 12, b = 0
Since b = 0, return a = 12 ✓
XOR truth table matches addition without carry:
a b | a^b | a+b (mod 2)
0 0 | 0 | 0
0 1 | 1 | 1
1 0 | 1 | 1
1 1 | 0 | 0 (carry happens)
AND finds where both bits are 1 (carry occurs):
a b | a&b | carry?
0 0 | 0 | No
0 1 | 0 | No
1 0 | 0 | No
1 1 | 1 | Yes!
Time Complexity: O(1)
Space Complexity: O(1)
class Solution {
public int getSum(int a, int b) {
while (b != 0) {
int sum = a ^ b; // XOR for sum without carry
int carry = (a & b) << 1; // AND and shift for carry
a = sum;
b = carry;
}
return a;
}
}
def getSum(a: int, b: int) -> int:
# Python requires masking for negative numbers
# 32-bit signed integer range
MAX = 0x7FFFFFFF # 2^31 - 1
MASK = 0xFFFFFFFF # 2^32 - 1
while b != 0:
a, b = (a ^ b) & MASK, ((a & b) << 1) & MASK
# Handle negative result
return a if a <= MAX else ~(a ^ MASK)
Note: Python has arbitrary precision integers, so we need masks to simulate 32-bit behavior.
class Solution {
public int getSum(int a, int b) {
if (b == 0) return a;
int sum = a ^ b;
int carry = (a & b) << 1;
return getSum(sum, carry);
}
}
In Java/C++, negative numbers are represented in two's complement:
Example: (-1) + 1 = 0
a = -1 = 11111111 (in 8-bit)
b = 1 = 00000001
XOR: 11111111 ^ 00000001 = 11111110
AND: 11111111 & 00000001 = 00000001
Carry: 00000001 << 1 = 00000010
a = 11111110, b = 00000010
... (continues until b = 0)
Result: 00000000 = 0 ✓
| Operator | Name | Description | Example |
|----------|------|-------------|---------|
| ^ | XOR | 1 if bits differ | 5 ^ 3 = 6 (101 ^ 011 = 110) |
| & | AND | 1 if both bits are 1 | 5 & 3 = 1 (101 & 011 = 001) |
| {'<'}{'<'} | Left Shift | Shift bits left, multiply by 2 | 5 {'<'}{'<'} 1 = 10 (101 << 1 = 1010) |
| {'>'}{'>'}{' '} | Right Shift | Shift bits right, divide by 2 | 5 {'>'}{'>'}{' '} 1 = 2 (101 >> 1 = 10) |
| ~ | NOT | Flip all bits | ~5 = -6 (in two's complement) |
a + (~b + 1) (two's complement)This problem demonstrates:
| instead of ^ for sumb != 0)+ or - operators (violates constraint!)Q: How would you subtract a - b?
a + (-b) where -b is two's complement: ~b + 1getSum(a, getSum(~b, 1))Q: What about multiplication?
Q: Why does left shift equal multiply by 2?
Q: How many iterations in worst case?
return b == 0 ? a : getSum(a ^ b, (a & b) << 1);
Tests understanding of:
This is a fundamental problem that appears in hardware design and low-level programming!
This problem uses several bit manipulation patterns:
a ^ b finds bits that differa & b finds bits that matchThese patterns appear in many bit manipulation problems!
Understanding this helps you understand how computers actually add numbers!