linux查看编译器的大小端,Linux系统大小端判断
大端模式
大端模式,是指數(shù)據(jù)的低位保存在內(nèi)存的高地址中,而數(shù)據(jù)的高位保存在內(nèi)存的低地址中。
小端模式
小端模式,是指數(shù)據(jù)的低位保存在內(nèi)存的低地址中,而數(shù)據(jù)的高位保存在內(nèi)存的高地址中。
判斷程序
文件:check_endian.c
編譯:gcc -o check_endian check_endian.c
#include // check system by char pointer
int check_system()
{
int n = 0x04030201;
if (*(char *)&n == 0x01)
{
return 1;
}
else
{
return 0;
}
return 0;
}
// check system by union
int check_system2()
{
union check
{
int i;
char c;
} u;
u.i = 0x04030201;
if (u.c == 0x01)
{
return 1;
}
else
{
return 0;
}
return 0;
}
int main(int argc, char *argv[])
{
// if (check_system() == 1)
if (check_system2() == 1)
{
printf("little endian.\n");
}
else
{
printf("big endian.\n");
}
return 0;
}
備注:
1)check_system和check_system2均可正常用于判斷大小端。
2)代碼亦可通過下面命令獲取:git clone https://github.com/wangfuyu/check_endian
總結(jié)
以上是生活随笔為你收集整理的linux查看编译器的大小端,Linux系统大小端判断的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自相关函数,功率谱,时间序列信号模型三者
- 下一篇: linux 其他常用命令