什么是“无符号温度:3'的结构或联合的意思吗? [英] What does 'unsigned temp:3' in a struct or union mean?

查看:166
本文介绍了什么是“无符号温度:3'的结构或联合的意思吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  这是什么C ++ code是什么意思?

我试图用映射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 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;
};

您可以看到一些变量被定义如无符号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).

的大小总是被调高至8每组符号XXX的倍数:YY;构建。

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'的结构或联合的意思吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆