任何使用位域的可移植代码似乎都能区分小端和大端平台.有关此类代码的示例,请参见declaration of struct iphdr in linux kernel.我不明白为什么比特端性是个问题.
据我所知,位域是纯粹的编译器构造,用于促进位级操作.
For instance, consider the following bitfield:
Here, writing
struct ParsedInt {
unsigned int f1:1;
unsigned int f2:3;
unsigned int f3:4;
};
uint8_t i;
struct ParsedInt *d = &i;
d->f2
is simply a compact and readable way of saying (i>>1) & (1<<4 - 1)
.
然而,bit操作是定义良好的,无论体系 struct 如何,都能正常工作.那么,为什么比特域不可移植呢?