请简述字节序的概念,并用联合体(共用体)的方式验证计算机是大端还是小端?
字节序是计算机存储多字节整数序列的方式
小端字节序:低序字节存储在低地址上
大端字节序:高序字节存储在低地址上
#include<stdio.h>
union test
{
unsigned int data;
char text[4];
};
int main(int argc, const char *argv[])
{
union test res;
res.data = 0x12345678;
if(res.text[0] == 0x78)
printf("小端存储\n");
else
printf("大端存储\n");
return 0;
}