小端或大端 [英] Little endian or Big endian
本文介绍了小端或大端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
union Endian
{
int i;
char c[sizeof(int)];
};
int main(int argc, char *argv[])
{
union Endian e;
e.i = 1;
printf("%d \n",&e.i);
printf("%d,%d,\n",e.c[0],&(e.c[0]));
printf("%d,%d",e.c[sizeof(int)-1],&(e.c[sizeof(int)-1]));
}
输出:
1567599464
1,1567599464,
0,1567599467
LSB存储在较低的地址中,而MSB存储在较高的地址中.这不是大端吗?但是我的系统配置将其显示为小端架构.
LSB is stored in the lower address and MSB is stored in the higher address. Isn't this supposed to be big endian? But my system config shows it as a little endian architecture.
推荐答案
您的系统肯定是little-endian
.如果是big-endian
,则以下代码:
You system is definitely little-endian
. Had it been big-endian
, the following code:
printf("%d,%d,\n",e.c[0],&(e.c[0]));
将为第一个%d
而不是1
打印0
.在little-endian
中,1存储为
would print 0
for the first %d
instead of 1
. In little-endian
1 is stored as
00000001 00000000 00000000 00000000
^ LSB
^Lower Address
,但在big-endian
中,它存储为
00000000 00000000 00000000 00000001
^LSB
^Higher Address
并且不要使用%d
打印变量的地址,请使用%p
.
And don't use the %d
to print addresses of variables, use %p
.
这篇关于小端或大端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文