Problem overview
The function getSum receives two Int values, a and b, and returns a + b without using the operators + and -. The lesson uses bitwise operations instead.
For a = 2 and b = 3 the answer is 5. In binary, 010 + 011: the middle column has a 1 in both numbers, so it leaves a 0 and carries a 1 to the left, and the sum is 101. Negative numbers work too: -5 + 3 is -2.
Approach: add the columns, then add the carries
Binary addition on paper handles one column at a time: 0 + 0 = 0, 0 + 1 = 1, and 1 + 1 = 10, which leaves 0 and carries 1 into the next column. Two bitwise operations capture this:
x xor yis the digit left in each column, with the carries ignored. It is 1 where exactly one of the bits is 1.(x and y) shl 1is the carries. A carry appears where both bits are 1, and it belongs one column to the left.
The sum without carries and the carries still add up to the full sum. So replace the pair (x, y) by (x xor y, carries) and repeat. Each round moves the lowest remaining carry at least one place left. An Int has 32 bits, so after at most 32 rounds the carries fall off the left end, y becomes 0, and everything is in x. The number of rounds doesn't grow with the values: the time is O(1).
Negative numbers need no special case. They are stored in two's complement, and this column-by-column addition is the same one the processor does.
Step by step
- Copy the parameters into two
vars,xset toaandyset tob, because Kotlin parameters are read-only.xholds the sum so far andythe carries still to add. - Loop while
y != 0. When no carries are left, there is nothing more to add. - Compute the carries in a
valnamedcarry:(x and y) shl 1. It must be computed first, because the next line changesx. - Add without carries:
x = x xor y. - Move the carries to
y:y = carry, so the next round adds them. - Return
x.
The solution in Kotlin
class Solution {
fun getSum(a: Int, b: Int): Int {
var x = a
var y = b
while (y != 0) {
val carry = (x and y) shl 1
x = x xor y
y = carry
}
return x
}
}
For a = 2 and b = 3 the loop takes two rounds:
round 1: x=1 y=4
round 2: x=5 y=0
For a = -5 and b = 3 it also takes two:
round 1: x=-8 y=6
round 2: x=-2 y=0
The Kotlin details worth knowing:
- Bitwise operators are words. Kotlin writes
and,xorandshlas infix functions instead of&,^and<<. - Parentheses. In
(x and y) shl 1they make the order explicit. Bothandandshlare named infix functions with the same precedence, so Kotlin evaluates a chain of them left to right. - Two's complement.
-5is stored as11111111111111111111111111111011. The loop treats it like any other 32 bits. Intwraps silently.getSum(Int.MAX_VALUE, 1)returns-2147483648, the same asInt.MAX_VALUE + 1does. The lesson's inputs stay between -1000 and 1000, so this never happens in its tests.
Complexity
Time: O(1). The loop runs at most 32 times, once per bit of an Int, whatever the inputs are.
Space: O(1). The function keeps three integers.
Edge cases and common mistakes
- Zero as an input. If
bis 0, the loop doesn't run, andais returned. - Opposite numbers.
getSum(7, -7)andgetSum(-1, 1)both return 0. - Computing
carryafter changingx. Oncexhas been replaced byx xor y, the AND no longer describes the columns where both bits were 1. - Forgetting the shift.
x and ymarks the columns that carry, but the carry goes into the next column to the left. Withoutshl 1the result is wrong. - Special-casing negatives. It isn't needed.
getSum(-1000, -1000)returns-2000with the same code.
Testing
a = 1,b = 2gives3.a = 2,b = 3gives5.a = -5,b = 3gives-2.a = 0,b = 0gives0.a = 1000,b = 1000gives2000.
