我是靠谱客的博主 神勇水池,这篇文章主要介绍位运算计算加法,现在分享给大家,希望可以做个参考。

class Solution{
public:
int Add(int num1,int num2)
{
int res1,res2;
int sum;
res1 = num1 ^ num2;
res2 = num1 & num2;
while(res2)
{
num1 = res1;
num2 = res2 << 1;
res1 = num1 ^ num2;
res2 = num1 & num2;
}
return res1;
}
};

代码为c++



1. 位运算加法 a+b = a^b + (a&b)<< 1

2. &: 与运算

  有0为0,无0为1

3. |: 或运算

有1为1,无1为0

4. ^: 异或运算

相同为0,不同为1

5. ~: 取反运算

1为0,0为1

6. <<:左移

7. >>:右移


参考来源:http://blog.csdn.net/zhongjiekangping/article/details/6855864

最后

以上就是神勇水池最近收集整理的关于位运算计算加法的全部内容,更多相关位运算计算加法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(117)

评论列表共有 0 条评论

立即
投稿
返回
顶部