Problem overview
The method getSum(a: Int, b: Int): Int returns a + b without using the operators + or -. Only bitwise operations are allowed, and the values lie between -1000 and 1000.
For a = 1 and b = 2 the answer is 3. For a = 2 and b = 3 it is 5, and for a = -5 and b = 3 it is -2.
Approach: add column by column
Add two binary numbers as you would on paper. In one column, 0 + 0 = 0, 0 + 1 = 1 and 1 + 1 = 10: a 0 stays in the column and a 1 is carried to the next column on the left. Two bit operations capture this:
- The digit left in each column is the XOR of the two bits:
x ^ yis the sum with the carries left out. - A carry appears only where both bits are 1, which is
x & y, and it belongs one column to the left, so the carries are(x & y) << 1.
The two results still add up to the original sum: x + y equals (x ^ y) + ((x & y) << 1). That is an addition again, but with carries that sit further left. Repeat the same step on the new pair. Each round pushes the lowest carry at least one place to the left, and after at most 32 rounds it falls off the end of an Int, so the carries become 0 and the sum is in x.
Step by step
- Copy
aandbintovar xandvar y, since Scala parameters can't be reassigned.xholds the sum so far andythe carries still to add. - Loop
while (y != 0): while a carry exists, there is something left to add. - Compute
val carry = (x & y) << 1first, beforexchanges. - Set
x = x ^ y. - Set
y = carry. - End the method with
x, the last expression.
The solution in Scala
class Solution {
def getSum(a: Int, b: Int): Int = {
var x = a
var y = b
while (y != 0) {
val carry = (x & y) << 1
x = x ^ y
y = carry
}
x
}
}
Scala details
- The order of the loop body matters:
carrymust be computed from the oldxandy, so it comes beforexis overwritten. carryis aval, because it is set once per round.xandyarevars.<<shifts left onIntand bits that go past position 31 are discarded, with no error. That is why the carries eventually vanish.- Negative numbers need no special handling. An
Intstores them in two's complement, and the bit-by-bit addition above is what the processor does itself. In two's complement, -5 is11111111111111111111111111111011.
Tracing two examples
For a = 2 and b = 3, the values after each round are:
round 1: x=1 y=4
round 2: x=5 y=0
In round 1, 2 ^ 3 is 1 (binary 01) and the carry is (2 & 3) << 1, which is 4. Round 2 adds 1 and 4 with no overlap, so x becomes 5 and the carry is 0.
For a = -5 and b = 3:
round 1: x=-8 y=6
round 2: x=-2 y=0
The loop ends with -2, the right answer for a negative operand.
Complexity
Time is O(1): the loop runs at most once per bit of an Int, 32 rounds, whatever the values are. Space is O(1): three integers.
Edge cases and common mistakes
- Zeros:
getSum(0, 0)never enters the loop and returns 0. If onlybis 0 the loop is skipped andacomes back. - Opposites:
getSum(-1, 1)andgetSum(1000, -1000)both return 0. The carry travels all the way to the left end and drops off. - Both negative:
getSum(-7, -9)returns -16. - Overflow: outside the lesson's range, results wrap like ordinary
Intaddition.getSum(Int.MaxValue, 1)returns -2147483648, the same asInt.MaxValue + 1. - Shifting the wrong value: shift the AND, not the XOR:
(x & y) << 1. Shiftingx ^ ygives a different number entirely.
