Re: [问题] big endian中bit field 的问题

楼主: descent (“雄辩是银,沉默是金”)   2022-12-28 15:32:41
一样用 qemu, 安装 ppc32 debein8 测试, 这个花了我比较多时间。
ref:
https://gist.github.com/nstarke/c6593da117c459c18c255395211fa311
ppc32
(gdb) p reg
$1 = {B1 = {A1 = 9 '\t', A2 = 0 '\000', A3 = 1 '\001', A4 = 1 '\001', A5 = 4
'\004', A6 = 22136}, B2 = 305419896 (0x12345678),
memory layout
(gdb) x/4xb &reg
0xbffffb28: 0x12 0x34 0x56 0x78
x86
(gdb) p reg
$2 = {B1 = {A1 = 18 '\022', A2 = 0 '\000', A3 = 1 '\001', A4 = 0 '\000',
A5 = 3 '\003', A6 = 30806}, B2 = 2018915346 (0x78563412), B3 = "\022\064Vx"}
memory layout
(gdb) x/32xb &reg
0xffffd058: 0x12 0x34 0x56 0x78
memory layout 都一样, 但 bit fields 的解释却不一样。
※ 引述《ca5270 (兰陵哭哭生)》之铭言:
: 各位大神好,小弟在最近的面试中碰到了这类的考题
: Q:在big endian中B1.A1的值会是多少
: 小弟的想法是因为在little endian中会取REG_SET.B3[0]这个内存中的低7位,值为0x12,
: 所以在big endian中会取内存中的高7位,所以值为0x9,请问这样的想法正确吗
: typedef unsigned char u8;
: typedef unsigned short u16;
: typedef unsigned long u32;
: typedef union{
: struct{
: u8 A1 :7;
: u8 :1;
: u8 A2 :2;
: u8 A3 :1;
: u8 A4 :1;
: u8 A5 :4;
: u16 A6;
: } B1;
: u32 B2;
: u8 B3[4];
: } REG_SET
: REG_SET.B3[0] = 0x12;
: REG_SET.B3[1] = 0x34;
: REG_SET.B3[2] = 0x56;
: REG_SET.B3[3] = 0x78;

Links booklink

Contact Us: admin [ a t ] ucptt.com