使用位运算符实现以下条件:交换两个数的值。

This commit is contained in:
flykhan 2023-06-20 08:52:14 +08:00
parent 485b93466b
commit 300c745459
1 changed files with 21 additions and 0 deletions

21
day6/homework/h10.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
int a = 1, b = 9;
cout << "a 原本二进制表示为: " << bitset<8>(a) << endl;
cout << "b 原本二进制表示为: " << bitset<8>(b) << endl;
a = a ^ b;
cout << "a 第一次转换二进制表示为: " << bitset<8>(a) << "\t此时 a 的值为: " << int(a) << endl;
b = a ^ b;
cout << "a 第一次转换二进制表示为: " << bitset<8>(a) << "\t此时 a 的值为: " << int(a) << endl;
a = a ^ b;
cout << "a 第一次转换二进制表示为: " << bitset<8>(a) << "\t此时 a 的值为: " << int(a) << endl;
cout << "转换后结果为: a = " << int(a) << "\tb = " << int(b) << endl;
return 0;
}