Avn*_*arr 5 c enums typedef options
我有一套可用的enumed选项
typdef enum {
option1 = 1 << 0,
option2 = 1 << 1,
option3 = 1 << 2,
} availableOptions;
Run Code Online (Sandbox Code Playgroud)
我希望在执行之前根据用户的输入将它们关闭和打开.
例如:
// iniatially set to all options
myOption = option1 | option2 | option3;
Run Code Online (Sandbox Code Playgroud)
//来自用户的一些输入之后
void toggleOption1()
{
// how can I toggle an option that was already set without impacting the other options
}
Run Code Online (Sandbox Code Playgroud)
使用逐位异或:
void toggleOption1()
{
myOption ^= option1;
}
Run Code Online (Sandbox Code Playgroud)
插入符号^是按位的XOR运算符.该声明:
a ^= b;
Run Code Online (Sandbox Code Playgroud)
仅翻转设置a相应位的位b.所有其他位都保持不变.