我有一组可用的枚举选项
typdef enum {
option1 = 1 << 0,
option2 = 1 << 1,
option3 = 1 << 2,
} availableOptions;
我想在执行它们之前根据用户的输入来关闭和打开它们。
例如:
// iniatially set to all options
myOption = option1 | option2 | option3;
//在用户输入之后
void toggleOption1()
{
// how can I toggle an option that was already set without impacting the other options
}
最佳答案
使用按位异或:
void toggleOption1()
{
myOption ^= option1;
}
插入符号 ^
是按位异或运算符。声明:
a ^= b;
仅翻转 a
中的位,其中设置了 b
中的相应位。所有其他位均保持不变。
关于c - 将枚举值切换为位标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20032057/