作者fmdjyl (小D(滴))
看板C_and_CPP
標題[問題] Byte資料個別定義
時間Fri Apr 3 00:29:44 2009
有一個問題想請教。
如果我有兩個BYTE 資料。
分別為 2bits 9 bits 2bits 2 bits 1bit
xxoooooo ooowwaae
由於那9bits是要做計算 0x000 ~ 0x1FF
又不要影響到 其他bits資料。
不知道該如何定義?(下面的定義不知道是否正確?)
謝謝大家
===============================
union{
unsigned int byte;
struct{
unsigned x:2;
unsigned o:9;
unsigned w:2;
unsigned a:2;
unsigned e:1;
} bit;
}PR;
PR.bit.x = 2;
PR.bit.o = 511;
PR.bit.w = 1;
PR.bit.a = 0;
PR.bit.e = 1;
PR.byte = 0xBFE9;
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.80.6
1F:推 VictorTom:看起來沒什麼問題:) 04/03 00:39
2F:→ VictorTom:等等, 看您xxooww的圖示, 這幾bit到底是往MSB還是LSB放? 04/03 00:41
3F:→ fmdjyl:x:bit 15-14 o:bit 13-5 w:bit 4-3 a:bit 2-1 e:bit 0 04/03 00:45
4F:→ fmdjyl:所以應該xxoooooo 為LSB ooowwaae 為MSB 04/03 00:46
5F:推 VictorTom:簡單說, 這樣定義, xx是在LSB, e會是在MSB.... 04/03 00:47
6F:→ VictorTom:另外, 只要兩BYTE, unsigned int應該可以換成unsigned 04/03 00:47
7F:→ VictorTom:short甚至是unsigned char byte[2]; 這樣:) 04/03 00:48
8F:→ VictorTom:?? x是bit 15-14, 那xxoooooo是MSB才對吧?? 04/03 00:48
9F:推 VictorTom:個人覺得應該 e:1; a:2; w:2; o:9; x:2; 這順序才是@_@" 04/03 00:55
10F:→ VictorTom:話說回來, 這個不確定要不要考慮大小印弟安....Orz 04/03 00:55
11F:推 sbrhsieh:要。bit-fields 最後的 layout 跟編譯器有關 04/03 01:25