作者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