Question: 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) {
int temp_a;
int temp_b;
do{
temp_a=a^b;//保存异或的结果
temp_b=(a&b)<<1;//保存需要进位的结果
a=temp_a;
b=temp_b;
}while(b!=0);
return a;
}
};