Created
June 30, 2019 23:43
-
-
Save Hanaasagi/81be42270efa1bf9ec9ec5bd30359ea7 to your computer and use it in GitHub Desktop.
判断字节序,返回 true 为 Little-Endian 否则为 Big-Endian
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int is_little_endian(void) { | |
union t | |
{ | |
int a; | |
char b; | |
} c; | |
c.a = 1; | |
return c.b == 1; | |
} | |
// 如果为 big endian,存储为下(Hex表示,int 占 32 bit,char 占 8 bit) | |
// 0000 0001 | |
// 所以以 char 读出时结果为 0 | |
// 如果为 little endian,存储为下 | |
// 0100 0000 | |
// 所以以 char 读出时结果为 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment