作者ca5270 (兰陵哭哭生)
看板C_and_CPP
标题[问题] big endian中bit field 的问题
时间Wed Dec 28 00:50:43 2022
各位大神好,小弟在最近的面试中碰到了这类的考题
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;
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 27.247.102.147 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1672159845.A.60D.html
1F:推 chuegou: 同一个byte里面不需要考虑大小头 12/28 01:46
2F:推 chuegou: 欸 不对 我没考虑到bitfield.... 12/28 01:50
3F:→ breeze08: 差点被骗+1 12/28 01:51
4F:推 lin130917: 0x21 吧 12/28 03:33
5F:推 lin130917: 我算错了 0x9 没错 12/28 03:34
6F:推 lc85301: 用 qemu power pc 测是 0x9 12/28 13:34
7F:推 SocketAM2: bitfield 是compiler impl defined吧,只给endian就考 01/01 08:52
8F:→ SocketAM2: 出题真的没问题吗? 01/01 08:52
9F:推 RadiationXen: bitfield的memory layout是implementation-defined 01/01 11:36
10F:推 james80351: 是群联考的吗?XD 01/03 00:21