在我看来,对于像 bool
这样的二进制变量,您只需要一位。明确告诉所有 bool
只使用 1 位是否是一个错误的决定?
struct Banana {
// little fields
bool on_off : 1;
bool yes_no : 1;
bool left_right : 1;
bool alive_dead : 1;
bool in_out : 1;
};
编辑:
我知道不能取一个字段的地址。还有其他缺点吗?
最佳答案
如果您有很多这样的东西,就可以节省空间。但它确实为正常的单字节解决方案的每个清除/设置/检查操作添加了至少一个额外的 AND
或 OR
指令。
在整个方案中,除非您实际拥有大量数据,否则可能没有任何好处。
关于c++ - 我可以一直使用 1 位 boolean 值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18249672/