结构或联合中的“无符号临时:3"是什么意思?

What does #39;unsigned temp:3#39; in a struct or union mean?(结构或联合中的“无符号临时:3是什么意思?)
本文介绍了结构或联合中的“无符号临时: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"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(将 RGB 转换为 HSV 并将 HSV 转换为 RGB 的算法,范围为 0-255)
How to convert an enum type variable to a string?(如何将枚举类型变量转换为字符串?)
When to use inline function and when not to use it?(什么时候使用内联函数,什么时候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意义;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相关吗?)