“unsigned temp:3” 是什么意思?在结构或联合中意味着什么?
可能的重复:
这段 C++ 代码是什么意思?
我正在尝试映射 C 结构使用 JNA 转换为 Java。我遇到了一些我从未见过的东西。
struct
定义如下:
struct op
{
unsigned op_type:9; //---> what does this mean?
unsigned op_opt:1;
unsigned op_latefree:1;
unsigned op_latefreed:1;
unsigned op_attached:1;
unsigned op_spare:3;
U8 op_flags;
U8 op_private;
};
您可以看到一些变量被定义为 unsigned op_attached:1
,我不确定这意味着什么。这会影响为此特定变量分配的字节数吗?
Possible Duplicate:
What does this C++ code mean?
I'm trying to map a C structure to Java using JNA. I came across something that I've never seen.
The struct
definition is as follows:
struct op
{
unsigned op_type:9; //---> what does this mean?
unsigned op_opt:1;
unsigned op_latefree:1;
unsigned op_latefreed:1;
unsigned op_attached:1;
unsigned op_spare:3;
U8 op_flags;
U8 op_private;
};
You can see some variable being defined like unsigned op_attached:1
and I'm unsure what would that mean. Would that effect the number of bytes to be allocated for this particular variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该构造指定每个字段的位长度。
这样做的好处是,如果你小心的话,你可以控制
sizeof(op)
。结构体的大小将是内部字段大小的总和。在您的情况下,op 的大小为 32 位(即
sizeof(op)
为 4)。对于每组无符号 xxx:yy,大小始终向上舍入为 8 的下一个倍数;构造。
这意味着:
我不确定我是否记得正确,但我认为我是对的。
This construct specifies the length in bits for each field.
The advantage of this is that you can control the
sizeof(op)
, if you're careful. the size of the structure will be the sum of the sizes of the fields inside.In your case, size of op is 32 bits (that is,
sizeof(op)
is 4).The size always gets rounded up to the next multiple of 8 for every group of unsigned xxx:yy; construct.
That means:
I'm not sure I remember this correctly, but I think I got it right.
它声明了一个位字段;冒号后面的数字给出了字段的位长度(即,使用多少位来表示它)。
It declares a bit field; the number after the colon gives the length of the field in bits (i.e., how many bits are used to represent it).
表示 op_type 是一个 9 位整型变量。
Means op_type is an integer variable with 9 bits.
整型上的冒号修饰符指定 int 应占用多少位。
The colon modifier on integral types specifies how many bits the int should take up.