使用位运算符实现以下条件:将一个数的第 n 位(从右往左数)设为 0。

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

17
day6/homework/h3.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;
}