看板 C_and_CPP 關於我們 聯絡資訊
各位大神好,小弟在最近的面試中碰到了這類的考題 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://www.ptt.cc/bbs/C_and_CPP/M.1672159845.A.60D.html
chuegou: 同一個byte裡面不需要考慮大小頭 12/28 01:46
chuegou: 欸 不對 我沒考慮到bitfield.... 12/28 01:50
breeze08: 差點被騙+1 12/28 01:51
lin130917: 0x21 吧 12/28 03:33
lin130917: 我算錯了 0x9 沒錯 12/28 03:34
lc85301: 用 qemu power pc 測是 0x9 12/28 13:34
SocketAM2: bitfield 是compiler impl defined吧,只給endian就考 01/01 08:52
SocketAM2: 出題真的沒問題嗎? 01/01 08:52
RadiationXen: bitfield的memory layout是implementation-defined 01/01 11:36
james80351: 是群聯考的嗎?XD 01/03 00:21