0
点赞
收藏
分享

微信扫一扫

sum of two integers

成义随笔 2022-12-13 阅读 81


​​https://leetcode.com/problems/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.

注释,这个题目有意思,学过FPGA的人都知道最基本的半加器和全加器是最基本的东西,这里也是这样计算加法的。

int getSum(int a, int b) {

if(b == 0)
return a;

int sum = a^ b;
int cout = (a & b) << 1;

return getSum(sum, cout);
}


举报

相关推荐

0 条评论