## Big-endian and Little-endian 用 C 的程式碼可以判斷記憶體中位元紀錄方式是 Big endian 還是 Little endian ```C #include <stdio.h> typedef union{ unsigned int l; unsigned char c[4]; }EndianTest; int main() { EndianTest et; et.l = 0x12345678; //printf("%ld",sizeof(long)); if(et.c[0] == 0x78) printf("This machine use little Endian\n"); else if(et.c[0] == 0x12) printf("This machine use Big endian\n"); for(int i=0; i < sizeof(int); i++) { printf("記憶體位置 : %p" , &et.c[i]); printf("的值為 %02x", et.c[i]); puts(""); } return 0; } ``` 用 union 做測試是因為 union 中 member 是共享同一區塊的memory, 可判斷為 Little or Big Endian. ### 以下為 Big Endian  ### 以下為 Little Endian  ### 處理器體系 * x86、MOS Technology 6502、Z80、VAX、PDP-11、RISC-V等處理器為小端序 (Little Endian); * Motorola 6800、Motorola 68000、PowerPC 970、System/370、SPARC(除V9外)等處理器為大端序 (Big Endian); * ARM、PowerPC(除PowerPC 970外)、DEC Alpha、SPARC V9、MIPS、PA-RISC及IA64的位元組序是可調整的。
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up