您的位置:首页 > Web前端

what is the difference between little-endian and big-endian.

2009-03-14 10:59 567 查看
老掉牙的问题,不过有时候往往因为这个问题而找不到问题的根源所在,最近就因为大小端问题害我浪费了半天时间,所以写下小记让自己多care一下这些细节问题。

1. 概念描述

little-endian 高字节在存储在高地址处,低字节存储在低地址处;

big-endian 高字节在存储在低地址处,低字节存储在高地址处。

简单的例子,对于一个32位的数0x88776655,保存在地址0x00000000处,对应的大小端存储格式如下:
big-endian little-endian
0x00000000 0x88 0x55
0x00000001 0x77 0x66
0x00000002 0x66 0x77
0x00000003 0x55 0x88

2. 判断所用的处理器类型

x86系列处理器都是用little-endian格式,PowerPC则使用big_endian格式,对于一些不太明确的处理器类型可以写个程序加以判别:

typedef union
{
unsigned char b1;
unsigned int t1;
} endien_t;

bool IsLittleEnd(void)
{
endien_t t;
t.b1 = 0x01;

return (t.t1&0xff);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: