“unsigned temp:3” 是什么意思?在结构或联合中意味着什么?

发布于 2024-09-03 12:53:29 字数 619 浏览 4 评论 0原文

可能的重复:
这段 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 技术交流群。

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

发布评论

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

评论(4

笔芯 2024-09-10 12:53:29

该构造指定每个字段的位长度。

这样做的好处是,如果你小心的话,你可以控制 sizeof(op)。结构体的大小将是内部字段大小的总和。

在您的情况下,op 的大小为 32 位(即 sizeof(op) 为 4)。

对于每组无符号 xxx:yy,大小始终向上舍入为 8 的下一个倍数;构造。

这意味着:

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)

我不确定我是否记得正确,但我认为我是对的。

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:

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.

昔梦 2024-09-10 12:53:29

它声明了一个位字段;冒号后面的数字给出了字段的位长度(即,使用多少位来表示它)。

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

翻身的咸鱼 2024-09-10 12:53:29
unsigned op_type:9;

表示 op_type 是一个 9 位整型变量。

unsigned op_type:9;

Means op_type is an integer variable with 9 bits.

耳根太软 2024-09-10 12:53:29

整型上的冒号修饰符指定 int 应占用多少位。

The colon modifier on integral types specifies how many bits the int should take up.

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