【发布时间】:2010-12-18 20:20:22
【问题描述】:
假设我需要在一个结构中存储 8 个布尔值,但我只想一起使用 1 个字节,那么我可以这样做:
struct myStruct {
bool b1:1;
bool b2:1;
bool b3:1;
bool b4:1;
bool b5:1;
bool b6:1;
bool b7:1;
bool b8:1;
};
有了这个我可以做类似的事情
myStruct asdf;
asdf.b3=true;
asdf.b4=false;
if(asdf.b1)
...
到目前为止这是正确的吗? (实际上我不知道,我以前从未使用过位域)
好的 - 但是是否也可以创建一个包含 8 个布尔值的静态数组,这样它们将只使用 8 位,但我仍然可以通过索引来寻址它们?
类似
struct myStruct {
public:
bool b[8]:8;
};
也许? (有了这个,我得到一个错误 C2033)
感谢您的帮助!
【问题讨论】:
标签: c++ struct boolean bit-fields