# C#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 -.

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

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

```public class Program {

public static void Main(string[] args) {
var a = 16;
var b = 13;

var res = GetSum(a, b);
Console.WriteLine(res);

a = 168;
b = 136;

res = GetSum2(a, b);
Console.WriteLine(res);

}

public static int GetSum(int a, int b) {
//按位取异或
int result = a ^ b;
//判断是否需要进位
int forward = (a & b) << 1;
if(forward != 0) {
//如有进位，则将二进制数左移一位，进行递归
return GetSum(result, forward);
}
return result;
}

public static int GetSum2(int a, int b) {
while(b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}

}```

```True
False```