几种位运算的含义在之前的文章有提及——
接下来我们主要关注异或运算
下面看到的是我们熟悉的swap语句
#include<iostream>
using namespace std;
int main()
{
int a, b; cin >> a >> b;
int temp = a;
a = b;
b = temp;
cout << a <<" "<< b;
return 0;
}
如果用异或运算怎么实现两个数字的交换呢
#include<iostream>
using namespace std;
int main()
{
int a, b; cin >> a >> b;
a = a ^ b;
b = a ^ b;
a = a ^ b;
cout << a << " " << b;
return 0;
}