描述
位运算有什么用呢,现在BobHuang想用位运算去实现a+b
我相信你们在计算机导论上已经对位运算初具了解,聪明的你们应该可以解答
输入
输入数据包含多个测试实例,每个测试实例占一行,包含两个数字a b
输出
输出为a+b的值
样例输入
1 2
3 4
100 201
样例输出
3
7
301
提示
operator的各种问题
分析:
这道题涉及的知识点蛮重要的,位运算是二进制操作的基本方式。
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
int c=a&b;//按位与
int d=a^b;//按位异或
while(c>0)
{
int C=c<<1;//按位左移
int D=d;
c=C&D;
d=C^D;
}
cout<<d<<endl;
}
return 0;
}
代码(a、b交换):
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
b=a^b;
a=a^b;
b=a^b;
cout<<a<<’ '<<b<<endl;
}
return 0;
}