一、术语来源
这两个术语来自于 Jonathan Swift 的《格利佛游记》其中交战的两个派别无法就应该从哪一端--小端还是大端--打开一个半熟的鸡蛋达成一致。
在那个时代,Swift是在讽刺英国和法国之间的持续冲突,Danny Cohen,一位网络协议的早期开创者,第一次
使用这两个术语来指代字节顺序,后来这个术语被广泛接纳了,成为计算机专用名词。除网络传输之外,在计算机硬件中也有使用,通常表示逻辑最小处理单元大于物理最小处理单元时逻辑单元与物理单元的映射方式。
二、大端与小端的区别
由于这个概念第一次提出时是来指代字节顺序,而且计算机物理最小处理单元通常为一个字节,所以通常情况下无论是大端还是小端都是以字节(8bit)计,在字节之内都是以大端顺序排列。但不排除以后随着计算机的发展将这个数字扩充。
| 字节排序 | 含义 |
|---|
| Big-Endian |
高位在前,低位在后。 |
| Little-Endian |
低位在前,高位在后 |
请看下面这个例子:
如果我们将0x1234abcd写入到以0x0000开始的内存中,则结果为
big-endian little-endian
0x0000 0x12 0xcd
0x0001 0x34 0xab
0x0002 0xab 0x34
0x0003 0xcd 0x12
然后,假如需要从内存中取32位整数0x1234abcd中的高16位整数,就需要知道是不是big-endian,如果是,需要从0x0002地址中去取,如果是little-endian,则需要从0x0000中取。也即怎么存就怎么取。
三、大端与小端的比较
Which is Better?
You may see a lot of discussion about the relative merits of the two formats,
mostly religious arguments based on the relative merits of the PC versus the Mac.
Both formats have their advantages and disadvantages.
In "Little Endian" form, assembly language instructions for picking up a 1, 2, 4, or longer byte number proceed
in exactly the same way for all formats: first pick up the lowest order byte at offset 0.
Also, because of the 1:1 relationship between address offset and byte number (offset 0 is byte 0),
multiple precision math routines are correspondingly easy to write.
In "Big Endian" form, by having the high-order byte come first,
you can always test whether the number is positive or negative by looking at the byte at offset zero.
You don't have to know how long the number is, nor do you have to skip over any bytes to find the byte containing the sign information.
The numbers are also stored in the order in which they are printed out, so binary to decimal routines are particularly efficient.
翻译如下:
在“小终结者”形式中,提取一个,两个,四个或者更长字节数据的汇编指令以与其他所有格式相同的方式进行:首先在偏移地址为0的地方提取最低位的字节,因为地址偏移和字节数是一对一的关系,多重精度的数学函数就相对地容易写了。
在“大终结者”的形式中,靠首先提取高位字节,你总是可以由看看在偏移位置为0的字节来确定这个数字是正数还是负数。你不必知道这个数值有多长,或者你也不必跳过一些字节来看这个数值是否含有符号位。这个数值是以它们被打印出来的顺序存放的,所以从二进制到十进制的函数特别有效。