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

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

17
day6/homework/h2.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 = x | (0x1 << n); // 作用: 将第 n 位设置为 1
cout << "修改后为: " << bitset<8>(x) << endl;
return 0;
}