Sum of Two Integers
Medium
Topics
Given two integers a and b, return their sum without using the operators + or -.
Example 1
Addition without
+: a ^ b adds the columns that do not carry, and (a & b) << 1 is the carry those columns produce. Repeating until the carry is zero gives 3.Cells are the binary expansion, most significant bit on the left; the small numbers are bit positions.
Input: a = 1, b = 2 Output: 3
Example 2
Input: a = 2, b = 3 Output: 5
Constraints
- -1000 <= a, b <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
Binary addition splits into two pieces: a ^ b adds bit-by-bit ignoring carries, and (a & b) << 1 is exactly the carry. Repeat until there is no carry left.
Approach
- Let
sum = a ^ b(addition without carry). - Let
carry = (a & b) << 1. - Set a = sum, b = carry, and loop until carry is 0.
Dry Run
-5 + 3, with no + or - anywhere
a = 11111111111111111111111111111011 (-5 in two's complement)
b = 00000000000000000000000000000011 (3)
round 1 a ^ b = 11111111111111111111111111111000 sum, ignoring carries
carry = 00000000000000000000000000000110 (a & b) shifted left
round 2 a ^ b = 11111111111111111111111111111110
carry = 00000000000000000000000000000000 no carry left, stop
a = 11111111111111111111111111111110
the top bit is set, so re-interpret as negative -> -2
answer -2
The two halves of addition are two separate bit operations. `a ^ b` is the sum of
each column ignoring carries; `(a & b) << 1` is exactly the carry, since a column
carries precisely when both bits are 1 and the carry lands one place left. Feeding
one into the other until the carry dies is binary addition, by definition.
It terminates because each round pushes the carry at least one bit further left,
so after at most 32 rounds it falls off the end. This example takes 2.
Two's complement is why negatives need no special case: -5 and 3 are added by the
same bit mechanics as 5 and 3. Python is the awkward language here, not the easy
one -- its integers are unbounded, so the carry never falls off anything and the
mask to 32 bits plus the final sign re-interpretation are doing work that Java,
C++ and JavaScript get from their native int width.Complexity
- Time — O(1): at most 32 carry-propagation iterations for fixed-width integers.
- Space — O(1): a constant number of variables.
Common Pitfalls
- In languages without wraparound (Python), mask to 32 bits and re-interpret the sign.
Full Solution — all languages
class Solution:
def getSum(self, a, b):
MASK = 0xFFFFFFFF
INT_MAX = 0x7FFFFFFF
a &= MASK
b &= MASK
while b:
carry = ((a & b) << 1) & MASK
a = (a ^ b) & MASK
b = carry
# Python ints are unbounded, so re-interpret the top bit as a sign
return a if a <= INT_MAX else ~(a ^ MASK)import java.util.*;
class Solution {
public int getSum(int a, int b) {
while (b != 0) {
int carry = (a & b) << 1;
a = a ^ b;
b = carry;
}
return a;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int getSum(int a, int b) {
unsigned int x = (unsigned int)a, y = (unsigned int)b;
while (y != 0) {
unsigned int carry = (x & y) << 1; // unsigned: shifting into the sign bit is defined
x = x ^ y;
y = carry;
}
return (int)x;
}
};/**
* @param {number} a
* @param {number} b
* @return {number}
*/ var getSum = function(a, b) {
// bitwise operators coerce to 32-bit signed ints, which is exactly what we want
while (b !== 0) {
const carry = (a & b) << 1;
a = a ^ b;
b = carry;
}
return a;
};
Asked at
Swiggy