在我之前的博文结构体中的位域和大小中,我测试过在我的编程环境下(我电脑型号为R9000P,AMD处理器,x86架构,win10操作系统,DevC++)的字节大小端和位大小端的情况。
先上代码
#include <stdio.h>
typedef union {
int a;
char b[4];
}Test;
int main() {
Test test1;
test1.a = 1;
printf("b arr first value : %d", test1.b[0]);
printf("b arr last value : %d", test1.b[3]);
return 0;
}
打印结果为:
b arr first value : 1
b arr last value : 0
可以看出tornado2.2下是字节小端模式。
继续可以用代码测试一下位的大小端:
#include <stdio.h>
typedef union {
int a;
struct {
unsigned int d1:1;
unsigned int d:30;
unsigned int d2:1;
}bitField;
}Test;
int main() {
Test test1;
test1.a = 1;
printf("d1:%d\n", test1.bitField.d1);
printf("d2:%d", test1.bitField.d2);
return 0;
}
打印结果为:
d1:1
d2:0
通过运行结果可知,此系统的位的大小端为小端模式,d1代表最低位LSB。
总结:字节和位都是小端模式。