371. Sum of Two Integers

Description

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example:
Given a = 1 and b = 2, return 3.

Credits:
Special thanks to @fujiaozhu for adding this problem and creating all test cases.

Solution

Bit manipulation

Basically, with key points:

exclusive or (^) handles these cases: 1+0 and 0+1
AND (&) handles this case: 1+1, where carry occurs, in this case, we’ll have to shift carry to the left, why? Think about this example: 001 + 101 = 110 (binary format), the least significant digits of the two operands are both ‘1’, thus trigger a carry = 1, with this carry, their least significant digits: 1+1 = 0, thus we need to shift the carry to the left by 1 bit in order to get their correct sum: 2

class Solution {
    public int getSum(int a, int b) {
        if (b == 0) {
            return a;
        }
        
        return getSum(a ^ b, (a & b) << 1);
    }
}

你可能感兴趣的:(371. Sum of Two Integers)