我有一个特殊的问题,我有一个16位结构。在结构内部,在成员“x_pos”之后,根据一些外部标志,接下来的5位代表不同的东西。
如果设置了外部标志,则它们是旋转&缩放参数(rotscale_param),否则高两位是水平和垂直翻转位。
我试图使用union来表示这个结构,但是当我如此sizeof(属性)时,我希望看到2个字节,但结果是4个字节。
我的代码:
typedef unsigned short u16;
struct attribute {
u16 x_pos : 9;
union {
u16 rotscale_param : 5;
struct {
u16 unused : 3;
u16 hflip : 1;
u16 vflip : 1;
};
};
u16 size : 2;
};
如果有帮助,我正在尝试为此结构制作C代码:
OBJ Attribute
Bit Expl.
0-8 X-Coordinate
When Rotation/Scaling used:
9-13 Rotation/Scaling Parameter Selection
When Rotation/Scaling not used:
9-11 Not used
12 Horizontal Flip
13 Vertical Flip
14-15 OBJ Size
以上报价来源:http://problemkaputt.de/gbatek.htm#lcdobjoamattributes
这是一个潜在的解决方案:
typedef struct attr_flag_set {
u16 x_pos : 9;
u16 rotscale_param : 5;
u16 size : 2;
};
typedef struct attr_flag_unset {
u16 x_pos : 9;
u16 unused : 3;
u16 hflip : 1;
u16 vflip : 1;
u16 size : 2;
};
union attribute_1 {
attr_flag_unset attr_unset;
attr_flag_set attr_set;
};
然而,我不确定这是否是理想的解决方案。
答案 0 :(得分:2)
要获得正确的包装并不容易
此外,该标准仅允许位填充,而声明属于相同类型
来自ISO / IEC 9899 C99,6.7.2.1 Structure and union specifiers
:
实现可以分配任何可寻址的存储单元 足以容纳一个位域。如果剩余足够的空间,那就是一个位域 紧跟在结构中的另一个位字段将被打包 进入相同单位的相邻位。如果剩余空间不足, 是否将不适合的位域放入下一个单元或 重叠相邻单元是实现定义的。的顺序 单位内的位域分配(高位到低位或 低阶到高阶)是实现定义的。对齐 可寻址存储单元未指定。
因此,当您在位域声明中插入位域结构时,这会破坏规则并保留另一个WORD进行存储 唯一的解决方法是将声明恢复为两种不同结构的并集:
#pragma pack(1)
union attribute
{
struct
{
u16 x_pos : 9; //See the homogeneous series of declarations
u16 rotscale_param : 5;
u16 size : 2;
} struct1;
struct
{
u16 x_pos : 9; //See the homogeneous series of declarations here also
u16 unused : 3;
u16 hflip : 1;
u16 vflip : 1;
u16 size : 2;
} struct2;
};
这适用于C99-C11
不要忘记指示编译器打包数据。正如Mah给我的注意,这可以通过大多数编译器上的pragma或其他编译器的特定语法来实现。而且在这种情况下,打包可以依赖于那些边界线应用程序编译
附:还要考虑字节顺序是依赖于编译器的,但实质上取决于CPU的字节顺序,因此在小端机器上运行良好的代码可能会在大端机器上失败。
如果需要完全可移植的代码,最佳做法是使用掩码和移位。
答案 1 :(得分:0)
如果您尝试使用hflip
vflip
创建位文件,我认为您打算执行以下操作:
struct attribute {
u16 x_pos : 9;
union {
u16 rotscale_param : 5;
struct {
u16 unused : 3,
hflip : 1,
vflip : 1;
} hvflip;
};
u16 size : 2;
};
您实际使用unused
,hflip
和vflip
从单个unsigned short
创建位域的位置。通过使用3完整unsigned shorts
来表示相关的5
位,如另一个答案所示,完全忽略了位域的好处/目的。您只需要1 unsigned-short
来表示16位。
这是一个简单的例子:
#include <stdio.h>
typedef unsigned short u16;
struct attribute {
u16 x_pos : 9;
union {
u16 rotscale_param : 5;
struct {
u16 unused : 3,
hflip : 1,
vflip : 1;
} hvflip;
};
u16 size : 2;
};
void binprnpad (const unsigned long v, size_t sz);
int main (void) {
struct attribute attr = { .x_pos = 0b101010101,
.rotscale_param = 0b10101,
.size = 0b10 };
printf ("\n attr.x_pos (%3hu) : ", attr.x_pos);
binprnpad (attr.x_pos, 9);
printf ("\n attr.rotscale_param (%3hu) : ", attr.rotscale_param);
binprnpad (attr.rotscale_param, 5);
printf ("\n attr.hvflip.unused (%3hu) : ", attr.hvflip.unused);
binprnpad (attr.hvflip.unused, 3);
printf ("\n attr.hvflip.hflip (%3hu) : ", attr.hvflip.hflip);
binprnpad (attr.hvflip.hflip, 1);
printf ("\n attr.hvflip.vflip (%3hu) : ", attr.hvflip.vflip);
binprnpad (attr.hvflip.vflip, 1);
printf ("\n attr.size (%3hu) : ", attr.size);
binprnpad (attr.size, 2);
printf ("\n\n");
return 0;
}
void binprnpad (const unsigned long v, size_t sz)
{
if (!sz) { fprintf (stderr, "error: invalid sz.\n"); return; }
if (!v) { putchar ('0'); return; }
while (sz--)
putchar ((v >> sz & 1) ? '1' : '0');
}
<强>输出强>
$ ./bin/struct_nested_bin
attr.x_pos (341) : 101010101
attr.rotscale_param ( 21) : 10101
attr.hvflip.unused ( 5) : 101
attr.hvflip.hflip ( 0) : 0
attr.hvflip.vflip ( 1) : 1
attr.size ( 2) : 10
如果您有任何问题,请告诉我。