位对齐,8 合 1
我正在尝试编译以下代码:
union Bool
{
bool b[8] : 8; // (1)
bool b0,b1,b2,b3,b4,b5,b6,b7 : 1;
};
但是行 (1) 无法编译,位对齐数组的语法是什么?
I'm trying to compile the following code:
union Bool
{
bool b[8] : 8; // (1)
bool b0,b1,b2,b3,b4,b5,b6,b7 : 1;
};
However the line (1) doesn't compile, whats the syntax for bit aligning an array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法在 C 中声明位数组。
数组的概念基于指针,并且只能具有指向字节的指针,而不能具有指向字节内各个位的指针。 C 位字段允许您将整数组件打包到比编译器默认情况下更少的内存中。数组不是整数,因此不能将数组打包到位字段中。如果您想阅读该标准,可以在 ISO/IEC 9899 - 编程语言 - C(查找§6.7.2.1)。
如果您需要速度,您可以使用布尔数组的并集,如果您需要紧凑的内存占用,您可以定义宏以提供对位字段的更方便的访问。
You can't declare an array of bits in C.
The concept of an array is based on a pointer, and you can only have pointers to bytes, not to individual bits within a byte. C Bit fields allow you to pack integer components into less memory than the compiler would by default. An array isn't an integer, so you can't pack an array into a bit field. If you want to read up on the standard, you can find it at ISO/IEC 9899 - Programming languages - C (look for §6.7.2.1).
If you need speed, you could use a union of an array of bools, and if you need a compact memory footprint, you could define macros to provide more convenient access to your bit fields.