Problem overview
The function get_sum receives two i32 values, a and b, and returns a + b without using the + or - operators. Only bitwise operations are allowed.
For a = 1 and b = 2 the answer is 3. For a = 2 and b = 3 it is 5. For a = -5 and b = 3 it is -2, so negative numbers must work too.
Approach: add the way you do on paper
Adding in binary column by column has two parts. In each column, 0 + 0 = 0, 0 + 1 = 1 and 1 + 1 = 10: a digit of 0 stays in the column and a 1 is carried to the next column on the left. The digit left in a column is the XOR of the two bits. A carry appears only where both bits are 1, which is the AND.
So a ^ b is the sum with all carries left out, and (a & b) << 1 is the carries, already moved one column left. Together they still add up to a + b. The sum has been split into two numbers, and the second one holds the work that remains. Repeat with the pair (sum without carries, carries) until the carries are 0. Then the first number is the complete sum.
For a = 2, b = 3 (010 and 011), the first pass gives a = 1, b = 4, and the second gives a = 5, b = 0. The answer is 5.
Step by step
- Loop while
b != 0. Hereais the sum so far andbthe carries still to add. When no carries are left, the sum is complete. - Compute the carries.
a & bkeeps the columns where both numbers have a 1, and<< 1moves each carry into the column it belongs to. This goes into a new variable,carry, beforeachanges. - Replace
awitha ^ b, the sum without the carries. - Replace
bwithcarry, so the next pass adds the carries to the newa. - Return
aas the last expression, without a semicolon.
The solution in Rust
impl Solution {
fn get_sum(mut a: i32, mut b: i32) -> i32 {
while b != 0 {
let carry = (a & b) << 1;
a = a ^ b;
b = carry;
}
a
}
}
Rust details worth knowing:
mutin the signature. The parameters are declaredmut a: i32, mut b: i32, so the function can reassign them. Withoutmut, the assignmentsa = a ^ bandb = carrywould not compile.- Negative numbers need no special case. An
i32is stored in two's complement, and the bit-by-bit addition above is the same one the processor performs. The callget_sum(-5, 3)returns-2. - A left shift doesn't overflow-panic.
x << 1simply drops the top bit. Rust panics for a shift amount of 32 or more, but not for bits that are shifted out. That is what makes the loop end: carries move left each pass and eventually fall off the end. - The standard way. Normal Rust code would write
a + b, which panics on overflow in a debug build and wraps in an optimized build. The explicit methoda.wrapping_add(b)says that wrapping is intended. This lesson's loop gives the wrapped result for any pair ofi32values.
Complexity
Time: O(1). Each pass moves the lowest carry at least one column to the left. An i32 has 32 columns, so after at most 32 passes the carries have fallen off the left end and b is 0. The count doesn't grow with the size of the values.
Space: O(1). The function keeps a, b and carry.
Edge cases and common mistakes
- Adding zero. With
b = 0the loop doesn't run andacomes back. - Opposite numbers.
get_sum(-1, 1)returns 0. All 32 bits of-1are 1, so the carry travels through every column before it falls off. - Mixed signs and two negatives.
get_sum(-5, 3)returns-2, andget_sum(-7, -8)returns-15. - Forgetting the shift. Using
a & bas the carry, without<< 1, puts every carry in the column where it was produced instead of the next one, so the result is wrong. - Overwriting
atoo early. Ifabecomesa ^ bbefore the carry is computed,a & bis computed from the wronga. That is whycarrycomes first.
Testing
a = 1,b = 2gives3.a = 2,b = 3gives5.a = -5,b = 3gives-2.a = 0,b = 0gives0.a = -1,b = 1gives0.a = -7,b = -8gives-15.a = 1000,b = -1000gives0.
