简体   繁体   English

C ++ bool数组为bitfield?

[英]C++ bool array as bitfield?

let's say i need to store 8 bools in a struct, but i want to use for them only 1 byte together, then i could do something like this: 假设我需要在结构中存储8个bool,但是我想一起使用它们只有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;
};

and with this i could do things like 有了这个我可以做的事情

myStruct asdf;
asdf.b3=true;
asdf.b4=false;
if(asdf.b1)
    ...

is this correct so far? 到目前为止这是正确的吗? (i don't know it actually, i never used bitfields before) (我实际上并不知道,我之前从未使用过bitfields)

ok - but is it also possible to create a static array of 8 bools such that they will use only 8 bits but i will still be able to adress them by index? 好的 - 但是也可以创建一个8个bool的静态数组,这样它们只能使用8位但是我仍然可以通过索引来加入它们吗?

something like 就像是

struct myStruct {
public:
    bool b[8]:8;
};

maybe? 也许? (with this, i get a error C2033) (有了这个,我得到一个错误C2033)

thanks for the help! 谢谢您的帮助!

I would recommend using a std::bitset That way you could simply declare: 我建议使用std::bitset这样你就可以简单地声明:

std::bitset<8> asdf;

and use it with []. 并与[]一起使用。

asdf[0] = true;
asdf[3] = false;

Wouldn't you rather use a byte data type to hold everything at once? 您是否宁愿使用byte数据类型一次保存所有内容? Then you'd only have to use logical AND s and OR s to get/put stuff into it. 然后你只需要使用逻辑ANDOR来获取/放入内容。 No struct required. 不需要struct

For various reasons I don't think it's a good idea - you're basically trying to replicate the behaviour of vector<bool> which has proven to be not a good idea . 出于各种原因,我认为这不是一个好主意 - 你基本上试图复制vector<bool>的行为,这已被证明不是一个好主意 If you're trying to do this only to save memory, I wouldn't bother. 如果您只是为了节省内存而尝试这样做,我不会打扰。 The overhead of accessing the various bools and extracting them from a bit field is likely to be a lot higher than what little memory you save unless you're extremely constrained by memory limits. 访问各种bool并从位字段中提取它们的开销可能比你保存的小内存高很多,除非你受到内存限制的极大限制。

To answer your direct question, if you want to do the bool/bitfield thing you'll have to use your first approach. 要回答你的直接问题,如果你想做bool / bitfield事情,你将不得不使用你的第一种方法。

Normally the legitimate and accepted use for the bitfield/bit twiddling approach is when you have to deal with hardware registers and are trying to either model a hardware register or actually access a hardware register after making it look like a memory location and superimposing a bit field structure over the register. 通常,当你必须处理硬件寄存器并试图建模硬件寄存器或实际访问硬件寄存器之后,使其看起来像存储器位置并叠加位字段时,合法和可接受的用途是bitfield / bit twiddling方法寄存器上的结构。

You may be able to get your compiler to do what you want, but sadly it isn't required. 您可以让编译器按照您的意愿执行操作,但遗憾的是它不是必需的。 For example, even a nice compiler that accepts the above might end up allocating an entire 32-bit word for your myStruct objects. 例如,即使一个接受上述内容的好编译器也可能最终为myStruct对象分配一个完整的32位字。

If you have the option, and you want that level of control over your types and how they are aligned and allocated, you should probably consider using Ada. 如果您有选项,并且您希望对类型以及它们的对齐和分配方式进行控制,那么您应该考虑使用Ada。 For example, the following works just fine in Ada: 例如,以下在Ada中工作得很好:

type Bit_Set is array (1..8) of Boolean;
for Bit_Set'size use 8;

High_Mask : constant Bit_Set := (1..7 => false, 8 => true);

...and you now have a single-byte bitmask, and the operators "and", "or", "xor", etc. that work bitwise with it. ...现在你有一个单字节的位掩码,以及与它一致的运算符“和”,“或”,“xor”等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM