What does #39;unsigned temp:3#39; in a struct or union mean?(结构或联合中的“无符号临时:3是什么意思?)
问题描述
可能的重复:
这段 C++ 代码是什么意思?
我正在尝试使用 JNA 将 C 结构映射到 Java.我遇到了一些我从未见过的东西.
I'm trying to map a C structure to Java using JNA. I came across something that I've never seen.
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,但我不确定这意味着什么.这会影响要为此特定变量分配的字节数吗?
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?
推荐答案
此构造指定每个字段的长度(以位为单位).
This construct specifies the length in bits for each field.
这样做的好处是你可以控制sizeof(op),如果你小心的话.结构的大小将是内部字段大小的总和.
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.
在您的情况下,op 的大小为 32 位(即 sizeof(op) 为 4).
In your case, size of op is 32 bits (that is, sizeof(op) is 4).
对于每组未签名的 xxx:yy,大小总是向上取整到下一个 8 的倍数;构造.
The size always gets rounded up to the next multiple of 8 for every group of unsigned xxx:yy; construct.
这意味着:
struct A
{
unsigned a: 4; // 4 bits
unsigned b: 4; // +4 bits, same group, (4+4 is rounded to 8 bits)
unsigned char c; // +8 bits
};
// sizeof(A) = 2 (16 bits)
struct B
{
unsigned a: 4; // 4 bits
unsigned b: 1; // +1 bit, same group, (4+1 is rounded to 8 bits)
unsigned char c; // +8 bits
unsigned d: 7; // + 7 bits
};
// sizeof(B) = 3 (4+1 rounded to 8 + 8 + 7 = 23, rounded to 24)
我不确定我是否记得正确,但我想我没记错.
I'm not sure I remember this correctly, but I think I got it right.
这篇关于结构或联合中的“无符号临时:3"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:结构或联合中的“无符号临时:3"是什么意思
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
