# LeetCode.371.Sum of Two Integers

## 题目描述

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.

## 解题过程

• ^被称为不进位的加号
• <<是一种进位或者乘法符号
• 公式：a + b = ( a ^ b ) + ( ( a & b ) << 1 )

class Solution {
public int getSum(int a, int b) {
int sum = a ^ b;//不考虑进位的和
int carry = (a & b)<<1;//进位
while(carry!=0){//直到进位为0，结束
int newsum = sum ^ carry;
carry = (sum & carry)<<1;
sum = newsum;
}
return sum;
}
}


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


316