#define FLAG_FAILED:1 在大多数人所知的“位标志”的意义上并不是真正的位标志。这也是糟糕的语法。
位标志通常被定义为你有一个 type 并且你通过“设置”它们来“打开”位。您通过“清除”标志来“关闭”它们。要比较位标志是否打开,请使用所谓的 bitwise 运算符 AND(例如 &)。
因此,您的 BIT0(例如 2^0)将被定义为BIT0 = 0x00000001,而 BIT1(例如 2^1)将被定义为BIT1 = 0x00000002。如果你想坚持使用 define,你可以通过设置和清除来做到这一点:
#ifndef setBit
#define setBit(word, mask) word |= mask
#endif
#ifndef clrBit
#define clrBit(word, mask) word &= ~mask
#endif
或作为模板
template<typename T>
inline T& setBit(T& word, T mask) { return word |= mask; }
template<typename T>
inline T& clrBit(T& word, T mask) { return word &= ~mask; }
如果你想设置位,可以这么说,你可以设置如下状态:
setBit(SystemState, SYSTEM_ONLINE);
或
setBit(SystemState, <insert type here>SYSTEM_ONLINE);
清除将是相同的,只需将 setBit 替换为 clrBit。
要比较,只需这样做:
if (SystemState & SYSTEM_ONLINE) { ... // do some processing
}
如果这是在 struct 中,则引用 struct。