有关结构和位的问题 [英] Question about struct and bits

查看:75
本文介绍了有关结构和位的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一天,我被要求回答一个关于struct的问题,它几乎是这样的:

One day, I was asked to answer a question about struct, it almost like this:

typedef struct TAG_DATA
{
    int type:2;
    int count:12;
    int reserved;
}DATA;
int main(int argc, char* argv[])
{
    DATA apple;
    apple.type  = 0x7;
    apple.count = 0x16;

    int *pValue;
    pValue = (int*)(&apple);
    printf("%x\n",*pValue);

    return 0;
}



打印的结果将是(?)
A: 0x13 B: 0x43
C: 0x1700 D: 0x4300 (对此项目不太清楚,但可以大致了解)

那时我无法给出答案,当我回到家时,仍然发现很难找到A,B,C或D的任何答案.在我的程序中,输出是0xccccc05b,为什么?

谁能告诉我有关这种价值分配的一些信息?



The result printed will be( ? )
A:0x13 B:0x43
C:0x1700 D:0x4300(Not very sure about this item,but sth approximately)

I can''t give an answer at that moment, and when I come back home, I still found it''s hard to figure out any answer of A,B,C,or D, In my program, ouput is 0xccccc05b, why ?

Who can tell me something about this kind of value assign? Thanks in advance!

推荐答案

C ++可以进行一些对齐优化,因此答案可能取决于编译器,甚至取决于编译器选项.
通常,位字段从右到左对齐,直到达到处理器字的倍数(32位,但是可以通过编译指示和开关更改对齐)(因此,该字段为跨字").

可能的布局可以是
C++ can do some alignment optimization, hence the answer can be compiler dependent, and even compiler-options dependent.

Normally bit fields are aligned from right to left until a multiple of the processor word (32 bit, but the alignment can be changed with pragmas and switches) is reached (so field is "across words").

A possible layout can be
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
|___________________________________|_______________________|___|
                                     ^count:12               ^type:2

|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
|_______________________________________________________________|
^ int reserved (:32)



如果您将所有这些都看做是单个int(*(int*)&value),
type = 7(二进制形式为111)将在type:2(使其等于3)中存储11,并且
count = 0x16 = 0001_0110会按原样存储它,因此您最终会遇到

101_1011 = 0x5B.但是还要考虑前面可能有许多未初始化的位,所以memset 数据变量在执行其他任何操作之前为全零.



If you look at all this as a single int, (*(int*)&value),
type = 7 (111 in binary) will store 11 in type:2 (making it = 3) and
count = 0x16 = 0001_0110 will store it as it is, so you will end-up with

101_1011 = 0x5B. But consider also that there could be a number of uninitialized bits in front, so memset the data variable to all-zero before doing anything else.


我看不到任何原因为什么答案应该是任何可能的值:
类型"是两位,因此分配值7应该使类型== 3
"count"是十二位,因此分配0x16的值应保留count == 22
未分配保留",因此值是随机的.
位故障:
I can''t see any reason why the answer should be any of the possible values:
"type" is two bits, so assigning a value of 7 should leave type == 3
"count" is twelve bits, so assigning a value of 0x16 should leave count == 22
"reserved" is unassigned, so the value is random.
Bit breakdown:
Bit number
 3         2         1         0 
10987654321098765432109876543210
..............................xx  type
..................xxxxxxxxxxxx..  count
xxxxxxxxxxxxxxxxxx..............  reserved

将值放在以下位置:

Puting the values in:

Bit number
 3         2         1         0
10987654321098765432109876543210
..............................11  type     == 0x03
..................000000010110..  count    == 0x16
xxxxxxxxxxxxxxxxxx..............  reserved == unassigned
xxxxxxxxxxxxxxxxxx00000001011011  Total of "apple"

因此,苹果将以0x05B结尾,顶部出现一些乱七八糟的内容.
即使出于某种奇怪的原因,编译器也会以其他方式打包位

So Apple will end 0x05B, with some random crap at the top.
Even if for some weird reason the compiler packed the bits the other way

Bit number
 3         2         1         0
10987654321098765432109876543210
11..............................  type     == 0x03
..000000010110..................  count    == 0x16
..............xxxxxxxxxxxxxxxxxx  reserved == unassigned
11000000010110xxxxxxxxxxxxxxxxxx  Total of "apple"

的总和,那么苹果将从0xC05开始,然后出现一些随机废话. br/> 我认为他们弄错了,也许测试是:他是否有信心说你错了"?

Then apple would start with 0xC05 then some random crap.
I think they got it wrong, and maybe the test was: Will he have the confidence to say "You are wrong"?


这篇关于有关结构和位的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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