# （Leetcode）371. Sum of Two Integers

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.

## Solution

### 思路

“^” XOR operation, 获得(a+b)的“和”in each bit
“&” AND operation, 获得(a+b)的“进位”in each bit.

### Code

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

• 本文已收录于以下专栏：

## Leetcode 371. Sum of Two Integers

371. Sum of Two Integers Total Accepted: 8805 Total Submissions: 17071 Difficulty: Easy Calc...

## 【题解】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 -. ...

## [LeetCode] 371. Sum of Two Integers

371. Sum of Two Integers(Easy) 常规解法，巧妙解法，内存机制？

## LeetCode 371.Sum of Two Integers 位运算实现加法(减法)

1.原题Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.(...

## LeetCode之路：371. Sum of Two Integers

LeetCode之路：371. Sum of Two Integers 一、引言这道题非常小巧，题干非常简洁： Calculate the sum of two integers a and b,...

## [leetcode]--371. Sum of Two Integers

Question 371： Calculate the sum of two integers a and b, but you are not allowed to use the operat...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)