C++布尔数组作为位域?

发布于 2024-08-11 18:01:49 字数 573 浏览 9 评论 0原文

假设我需要在一个结构中存储 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 个 bool 的静态数组,这样它们只使用 8 位,但我仍然能够通过索引来寻址它们?

也许吧

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

? (这样,我收到错误 C2033)

感谢您的帮助!

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:

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)

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?

something like

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

maybe? (with this, i get a error C2033)

thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟织青萝梦 2024-08-18 18:01:49

我建议使用 std::bitset这样你就可以简单地声明:

std::bitset<8> asdf;

并将其与 [] 一起使用。

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

I would recommend using a std::bitset That way you could simply declare:

std::bitset<8> asdf;

and use it with [].

asdf[0] = true;
asdf[3] = false;
疯狂的代价 2024-08-18 18:01:49

您不想使用 byte 数据类型来同时保存所有内容吗?然后,您只需使用逻辑 AND 和 OR 来获取/放入内容即可。不需要struct

Wouldn't you rather use a byte data type to hold everything at once? Then you'd only have to use logical ANDs and ORs to get/put stuff into it. No struct required.

感性不性感 2024-08-18 18:01:49

由于各种原因,我认为这不是一个好主意 - 您基本上是在尝试复制 vector 的行为,该行为已被证明是 不是一个好主意。如果你这样做只是为了节省内存,我不会打扰。访问各种布尔值并从位字段中提取它们的开销可能比您节省的内存要高得多,除非您受到内存限制的极大限制。

要回答您的直接问题,如果您想做 bool/bitfield 的事情,您必须使用第一种方法。

通常,位域/位旋转方法的合法且可接受的用途是当您必须处理硬件寄存器并尝试对硬件寄存器进行建模或在使其看起来像内存位置并叠加位域后实际访问硬件寄存器时寄存器上的结构。

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. 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.

To answer your direct question, if you want to do the bool/bitfield thing you'll have to use your first approach.

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.

段念尘 2024-08-18 18:01:49

您也许可以让编译器执行您想要的操作,但遗憾的是这不是必需的。例如,即使是接受上述内容的优秀编译器也可能最终为您的 myStruct 对象分配整个 32 位字。

如果您可以选择,并且希望对类型以及它们的对齐和分配方式进行一定程度的控制,那么您可能应该考虑使用 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”、“or”、“xor”等。

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.

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. For example, the following works just fine in 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文