Problem overview
The method getSum takes two int values a and b and returns a + b without using the operators + and -. The statement's values stay between -1000 and 1000, and negative numbers must work too.
For a = 2 and b = 3 the answer is 5. For a = -5 and b = 3 it is -2.
The statement asks for O(1) time and space: the loop may run once per bit of the integer type, and no more.
Approach: add column by column
Think of adding on paper in binary. In each column, 0 + 0 = 0, 0 + 1 = 1, and 1 + 1 = 10: a 0 stays in the column and a 1 is carried into the next one. So the digit left in each column is the XOR of the two bits, and a carry appears exactly where both bits are 1, which is AND, one column to the left.
That gives two numbers: a ^ b, the sum without carries, and (a & b) << 1, the carries. Their sum is still a + b. So repeat with them as the new a and b: XOR them, compute their carries, and stop when no carry is left. Then the whole sum is in a.
Each round moves the lowest carry at least one place left, and an int has 32 bits, so after at most 32 rounds it falls off the left end and b becomes 0.
For a = 2 and b = 3 the lesson's loop goes through these states:
a=1 b=4
a=5 b=0
The same loop on a = -5 and b = 3:
a=-8 b=6
a=-2 b=0
Negative numbers need no special case: they are stored in two's complement, and this column-by-column addition is the one a processor does.
Step by step
- Loop while
b != 0.aholds the sum so far andbthe carries still to add. - Compute the carries into an
intnamedcarry: the bits set in both numbers,a & b, shifted left by one. The lesson convertsa & btounsigned intbefore shifting. - Set
atoa ^ b, the sum with the carries left out. - Set
btocarry, so the next round adds the carries toa. - Return
a.
The solution in C++
class Solution {
public:
int getSum(int a, int b) {
while (b != 0) {
int carry = (unsigned int)(a & b) << 1;
a = a ^ b;
b = carry;
}
return a;
}
};
C++ details worth knowing:
- Why the
unsigned intcast. In C++17, shifting a negativeintto the left is not allowed (the behaviour is undefined). Whenaandbare both negative,a & bis negative. Converting it tounsigned intfirst makes the shift well defined: bits that fall off the left end are dropped. - Storing back in an
int. The shifted value is converted toint, which keeps the same 32 bits, so a carry into the top bit becomes the sign bit, as two's complement requires. - Precedence of the cast. A cast binds tighter than
<<, so the cast applies to(a & b)and the shift happens on the unsigned value. - Parameters are copies.
aandbare passed by value, so the loop can change them without touching the caller's variables.
Complexity
Time: O(1). The loop runs at most once per bit of the int, 32 times, whatever the input.
Space: O(1). Only carry and the two parameters are used.
Edge cases and common mistakes
- Adding zero. With
b = 0the loop doesn't run andais returned as it is.getSum(0, 0)returns 0. - Opposite numbers.
getSum(-1, 1)andgetSum(1000, -1000)both return 0. The carries run through the whole number before they fall off. - Two negatives.
getSum(-7, -8)returns -15. This is the case wherea & bis negative and the cast matters. - Forgetting the shift. A carry belongs to the next column. Adding
a & bwithout shifting gives wrong sums. - Looping on
a. The loop ends when the carries are gone,b == 0, not whenareaches some value.
Testing
a = 1,b = 2gives 3.a = 2,b = 3gives 5.a = -5,b = 3gives -2.a = -1,b = 1gives 0.a = 1000,b = -1000gives 0.a = -7,b = -8gives -15.
