Problem overview
The method getSum receives two integers, a and b, and returns a + b without using the operators + and -. The values are between -1000 and 1000, and the note asks for O(1) time and space, which means it may loop once per bit of the integer type but no more.
For a = 1 and b = 2 the binary addition is 01 + 10: no column has a 1 in both numbers, nothing is carried, and the sum is 11, which is 3. For a = 2 and b = 3, 010 + 011 has a 1 in both numbers in the middle column, which leaves a 0 and carries a 1 to the left, giving 101, which is 5. Negative numbers work too: -5 + 3 is -2.
Approach: add column by column, as on paper
When you add two binary numbers on paper, each column gives a digit and possibly a carry: 0 + 0 = 0, 0 + 1 = 1, and 1 + 1 = 10, a 0 in that column and a 1 carried into the next one on the left.
- The digit left in each column is the XOR of the two bits:
a ^ bis the sum with all the carries left out. - A carry appears only where both bits are 1, which is
a & b, and it belongs one column to the left, so the carries are(a & b) << 1.
These two numbers still add up to a + b, so we can repeat the step with them: XOR them, compute their carries, and stop when no carry is left. Each round moves the lowest carry at least one place to the left, and the bitwise operators work on 32-bit integers, so after at most 32 rounds the carries fall off the left end. That number doesn't depend on the input, which is why the time is O(1).
Here are the rounds for a = 2 and b = 3, showing the values of a and b at the end of each round:
round a b
1 1 4
2 5 0
Once b is 0, a holds the sum, 5.
Step by step
- Loop while
b !== 0. Hereaholds the sum so far andbthe carries still to add. When there are none, the sum is all ina. - Compute the carries:
(a & b) << 1. The AND keeps a bit only where both numbers have a 1, and the shift moves each one place left. - Add without carries:
a = a ^ b. Each column is added on its own, and 1 + 1 gives 0. The carry that column should make was saved in the previous step. - Set
bto the carries, so the next round adds them toa. The valuea + bstays the same from round to round: part of it just moves from one number to the other. - Return
a.
The solution in TypeScript
class Solution {
getSum(a: number, b: number): number {
while (b !== 0) {
let carry = (a & b) << 1;
a = a ^ b;
b = carry;
}
return a;
}
}
Some TypeScript details:
- Negative numbers need nothing special. The bitwise operators convert their operands to signed 32-bit integers in two's complement, which is how processors add. For
a = -5andb = 3the rounds givea = -8, b = 6and thena = -2, b = 0, so the result is-2. - The carry is computed first.
carrymust be calculated beforeachanges, because it needs the olda. Swapping the order of the first two lines in the loop would give wrong results. - The result stays in 32 bits. Every bitwise result is a signed 32-bit integer, so the method is exact for 32-bit values. For example
getSum(2147483647, 1)returns-2147483648, the same wrap-around a 32-bitintwould show, but that is far outside this lesson's range. - Parameters are reassigned.
aandbare local variables, so the loop can overwrite them without affecting the caller. - Types are erased.
numberdoesn't promise integers. The bitwise operators would truncate a value like1.5to1.
Complexity
Time: O(1). Each round pushes the lowest carry at least one place to the left, and after at most 32 rounds the carries are gone. Each round does a fixed number of operations.
Space: O(1). The method keeps carry and reuses its two parameters.
Edge cases and common mistakes
- Zeros.
getSum(0, 0)returns0, andgetSum(7, 0)skips the loop and returns7. - Opposite numbers.
getSum(-1, 1)andgetSum(7, -7)both return0: the carries run off the left end of the 32 bits. - Both negative.
getSum(-1000, -1000)returns-2000, andgetSum(1000, 1000)returns2000. - Shifting the wrong value. The carry is
(a & b) << 1. Without the shift, the carry would be added back into the same column and the loop would never finish. - Using
|instead of^. OR gives 1 for 1 + 1, which is wrong: the digit must be 0, with the carry going left. - Parentheses.
a & b << 1would shiftbfirst, because<<binds tighter than&. Keep the parentheses arounda & b.
Testing
a = 1,b = 2gives3.a = 2,b = 3gives5.a = -5,b = 3gives-2.a = 0,b = 0gives0.a = -1,b = 1gives0.a = -1000,b = -1000gives-2000.a = 1000,b = 1000gives2000.
