将一个数的第 n 位(从右往左数)翻转,即如果原来是 0,变为 1;如果原来是 1,变为 0。

This commit is contained in:
flykhan 2023-06-20 08:49:55 +08:00
parent acabbbf714
commit ffb080d49f
1 changed files with 17 additions and 0 deletions

17
day6/homework/h4.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
char x = 0b01011011; // char 正好是八位
cout << "原值为: " << bitset<8>(x) << endl;
int n;
cout << "请输入要修改的位: ";
cin >> n;
x ^= (0x1 << n); // 作用: 将第 n 位设置为 0
cout << "修改后为: " << bitset<8>(x) << endl;
return 0;
}