大端字节小端字节

Big Endian

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

AddressValue
100090
1001AB
100212
1003CD
Little Endian

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

AddressValue
1000CD
100112
1002AB
100390


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

编程确定机器属于大端字节序还是小端字节序
#include <stdio.h>

int
main(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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值