C++ 将第n位更改为x

示例

C风格的位操作

// 如果x为1,则将设置n位;如果x为0,则将清零。
number ^= (-x ^ number) & (1LL << n);

使用std :: bitset

set(n,val)-将bitn设置为值val。

std::bitset<5> num(std::string("00100"));
num.set(0,true);  // num现在是00101
num.set(2,false); // num现在是00001