LeetCode 371. Sum of Two Integers
Created at 2017-11-03 Updated at 2017-11-07 Category LeetCode
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Given a = 1 and b = 2, return 3.
Special thanks to @fujiaozhu for adding this problem and creating all test cases.
Solve it in bitwise, and make sure both x and y are >= zero.
- Step 1: If you ignore the carry, say 0+0=1, 0+1=1, 1+0=1, 1+1=0, this is xor operation, ^
- Step 2: Apparently, 1+1 you will get 10, that is 10=(1&1)<<1.
- Step 3: Add the return of step 1 and step 2, and continue step 1 and step 2 until carry is 0.