Leetcode 371. Sum of Two Integers (Easy) (cpp)
Tag: Bit Manipulation
Difficulty: Easy
/*
371. Sum of Two Integers (Easy)
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.
*/
class Solution {
public:
int getSum(int a, int b) {
while (b){
int x = a ^ b, y = (a & b) << 1;
a = x; b = y;
}
return a;
}
};