大端字节小端字节

来源:互联网 发布:js 子字符串 编辑:程序博客网 时间:2024/06/13 10:32

Big Endian

In big endian, you store the most significant byte in the smallest address. Here's how it would look:

AddressValue1000901001AB1002121003CD

Little Endian

In little endian, you store the least significant byte in the smallest address. Here's how it would look:

AddressValue1000CD1001121002AB100390

大端字节序:高位字节存在小地址;小端字节序:低位字节存在小地址。

编程确定机器属于大端字节序还是小端字节序
#include <stdio.h>intmain(int argc, char **argv){union{short s;char c[sizeof(short)];}un;un.s = 0x0102;printf("sizeof(un) = %d\n",sizeof(un));if (sizeof(short) == 2) {if(un.c[0] == 1 && un.c[1] == 2)printf("big-endian\n");else if(un.c[0] == 2 && un.c[1] == 1)printf("little-endian\n");else printf("unknow\n");}else {printf("sizeof(short) = %d\n", sizeof(short));}return 0;}


0 0
原创粉丝点击