大字节序和小字节序的由来
Endian的由来
链接:
一、引子............................................................................................................................ 2 二、ENDIAN的由来 ............................................................................................................ 2 三、各种ENDIAN ............................................................................................................... 3 四、收尾............................................................................................................................ 5 五、REFERENCES & WEBLINKS ......................................................................................... 5 regularly, neat in appearance, the main-beam with small harness line must be smooth transition, small wire harness tied to main beam should be at a 90-degree angle. 6.4.5 harness banding material for plastic cable tie, and banding material colors should
一、引子
在各种计算机体系结构中,对于字节、字等的存储机制有所不同,因而引发了
计算机通信领域中一个很重要的问
,即通信双方交流的信息单元(比特、字节、
字、双字等等)应该以什么样的顺序进行传送。如果不达成一致的规则,通信双方
将无法进行正确的编/译码从而导致通信失败。目前在各种体系的计算机中通常采
用的字节存储机制主要有两种:
big-endian和little-endian。本文简要描述这两种存储机制的来历、特点和区别。
为了叙述方便,下面先对本文中将要用到的两个术语做简单的定义。
1、MSB
MSB是Most Significant Bit/Byte的首字母缩写,通常译为最重要的位或者最
重要的字节。它通常用来
明在一个bit序列(如一个byte是8个bit组成的一个序
列)或者一个byte序列(如word是两个byte组成的一个序列)中对整个序列取值影
响最大的那个bit/byte。
2、LSB
LSB是Least Significant Bit/Byte的首字母缩写,通常译为最不重要的位或
者最不重要的字节。它通常用来表明在一个bit序列(如一个byte是8个bit组成的
一个序列)或者一个byte序列(如word是两个byte组成的一个序列)中对整个序
列取值影响最小的那个bit/byte。
二、endian的由来
1、Definition
endian: The ordering of bytes in a multi-byte number. 定义:在计算机系统体系结构中用来描述在多字节数中各个字节的存储顺序。
2、Etymology
The term comes from Swift's "Gulliver's Travels" via the famous paper
"On Holy Wars and a Plea for Peace" by Danny Cohen, USC/ISI IEN 137,
1980-04-01.
The Lilliputians, being very small, had correspondingly small political
problems. The Big-Endian and Little-Endian parties debated over whether
soft-boiled eggs should be opened at the big end or the little end.[From:
Free On-Line Dictionary Of Computing or Jargon File]
词源:据Jargon File记载,endian这个词来源于Jonathan
Swift在1726年写的讽刺小说 "Gulliver's Travels"(《格利佛游记》)。该小说
在描述Gulliver畅游小人国时碰到了如下的一个场景。在小人国里的小人因为非常
astic cable tie, and banding material colors shoulddegree angle. 6.4.5 harness banding material for pl-beam with small harness line must be smooth transition, small wire harness tied to main beam should be at a 90-regularly, neat in appearance, the main2
小(身高6英寸)所以总是碰到一些意想不到的问题。有一次因为对水煮蛋该从大的
一端(Big-End)剥开还是小的一端(Little-End)剥开的争论而引发了一场战争,
并形成了两支截然对立的队伍:支持从Big-End剥开的人Swift就称作Big-Endians 而支持从Little-End剥开的人就称作Little-Endians......(后缀ian表明的就是支持
某种观点的人:-)。Endian这个词由此而来。
1980年,Danny Cohen在其著名的论文"On Holy Wars and a Plea for Peace" 中为了平息一场关于在消息中字节该以什么样的顺序进行传送的争论而引用了该词。
该文中,Cohen非常形象贴切地把支持从一个消息序列的MSB开始传送的那伙人叫做
Big-Endians,支持从LSB开始传送的相对应地叫做Little-Endians。此后Endian这
个词便随着这篇论文而被广为采用。
三、各种endian
1、big-endian
A computer architecture in which, within a given multi-byte numeric
representation, the most significant byte has the lowest address (the
word is stored "big-end-first").
Most processors, including the IBM 370 family, the PDP-10, the
Motorola microprocessor families, and most of the various RISC designs
current in mid-1993, are big-endian. [From: Free On-Line Dictionary Of
Computing or Jargon File]
big-endian:计算机体系结构中一种描述多字节存储顺序的术语,在这种机制
中最重要字节(MSB)存放在最低端的地址上。采用这种机制的处理器有IBM3700系
列、PDP-10、Mortolora微处理器系列和绝大多数的RISC处理器。
+----------+
| 0x34 |<-- 0x00000021
+----------+
| 0x12 |<-- 0x00000020
+----------+
图1:双字节数0x1234以big-endian的方式存在起始地址0x00000020中
在Big-Endian中,对于bit序列中的序号编排方式如下(以双字节数0x8B8A为例):
bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+----------------------------------------+ val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 |
+----------------------------------------+
^ 0x8B 0x8A ^
MSB LSB
图2:Big-Endian的bit序列编码方式
al for plastic cable tie, and banding material colors shoulddegree angle. 6.4.5 harness banding materi-beam with small harness line must be smooth transition, small wire harness tied to main beam should be at a 90-regularly, neat in appearance, the main3
注1:通常在TCP/IP
栈所说的网络序(Network Order)就是遵循Big-Endian 规则。在TCP/IP网络通信中,通信双方把消息按照如图2的方式进行编码,然后按
从MSB(Bit0)到LSB的顺序在网络上传送。
2、little-endian
A computer architecture in which, within a given 16- or 32-bit word,bytes at lower addresses have lower significance (the
word is stored "little-end-first"). The PDP-11 and VAX families of
computers and Intel microprocessors and a lot of communications and
networking hardware are little-endian.
The term is sometimes used to describe the ordering of units other
than bytes; most often, bits within a byte. [From: Free On-Line Dictionary
Of Computing or Jargon File]
little-endian:计算机体系结构中一种描述多字节存储顺序的术语,在这种机
制中最不重要字节(LSB)存放在最低端的地址上。采用这种机制的处理器有PDP-11、 VAX、Intel系列微处理器和一些网络通信设备。该术语除了描述多字节存储顺序外
还常常用来描述一个字节中各个比特的排放次序。
+----------+
| 0x12 |<-- 0x00000021
+----------+
| 0x34 |<-- 0x00000020
+----------+
图3:双字节数0x1234以little-endian的方式存在起始地址0x00000020中
在Little-Endian中,对于bit序列中的序号编排和Big-Endian刚好相反,其方 式如下(以双字节数0x8B8A为例):
bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 +-----------------------------------------+ val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 | +-----------------------------------------+ ^ 0x8B 0x8A ^
MSB LSB
图4:Little-Endian的bit序列编码方式
注2:通常我们说的主机序(Host Order)就是遵循Little-Endian规则。所以
当两台主机之间要通过TCP/IP协议进行通信的时候就需要调用相应的
进行主机
序(Little-Endian)和网络序(Big-Endian)的转换。
astic cable tie, and banding material colors shoulddegree angle. 6.4.5 harness banding material for pl-beam with small harness line must be smooth transition, small wire harness tied to main beam should be at a 90-regularly, neat in appearance, the main4
注3:正因为这两种机制对于同一bit序列的序号编排方式恰恰相反,所以《现
代英汉词典》中对MSB的翻译为"最高有效位"欠妥,故本文定义为"最重要的bit
/byte"。
3、middle-endian:
Neither big-endian nor little-endian. Used of perverse byte orders such as 3-4-1-2 or 2-1-4-3, occasionally found in
the packed decimal formats of some minicomputer manufacturers.[From:
Free On-Line Dictionary Of Computing or Jargon File]
middle-endian:除了big-endian和little-endian之外的多字节存储顺序就是
middle-endian,比如以4个字节为例:象以3-4-1-2或者2-1-4-3这样的顺序存储的 就是middle-endian。这种存储顺序偶尔会在一些小型机体系中的十进制数的压缩格
式中出现。
四、收尾
要详细解释这两种编码顺序已经超出本文所涉及的内容,如果你有兴趣的话可
以参考上面提及的Danny Cohen的论文("On Holy Wars and a Plea for Peace"), 该论文详细的描述了这两种编码顺序的历史、所基于的数学理论和各自拥护者争论
的焦点等知识,绝对可以大饱你打破沙锅问到底的内心需要。
五、References & WebLinks
1. Free On-Line Dictionary Of Computing [] 2. Jargon File [] 3. Gulliver's Travels《格利佛游记》
[] 4. On Holy Wars and a Plea for Peace
[]
---------------------------------------------------------------------------
关于大字节序和小字节序
大字节序:把高有效位放在低地址段,例如在按字节寻址的存储器中往地址 0x0001 存放值 0x12345678,在存储器中为
地址 数值
0x0004 0x78
0x0003 0x56
al for plastic cable tie, and banding material colors shoulddegree angle. 6.4.5 harness banding materi-beam with small harness line must be smooth transition, small wire harness tied to main beam should be at a 90-regularly, neat in appearance, the main5
0x0002 0x34
0x0001 0x12
小字节序:把低有效位放在低地址段,例如在按字节寻址的存储器中往地址 0x0001 存放值 0x12345678,在存储器中为
地址 数值
0x0004 0x12
0x0003 0x34
0x0002 0x56
0x0001 0x78
在传输半字,字,双字的时候,读取地址时总是由低往高,通常 x86 等指令集用的是
【注1】小字节序,RISC等其他指令集用大字节序。在同一指令集中进行数据传输不需要进行字节序转换,在不同的指令集之间进行传输时需要进行字节序转换,如进行网络传输。网络传输用的都是大字节序。
历史:网络由 University of California Berkeley 开发,当时用的是 RISC 指令集。为大字节序,从此以后网络传输为大字节序。
【注1】RISC(reduced instruction set computer,精简指令集计算机)是一种执行较少类型计算机指令的微处理器,起源于80 年代的MIPS主机(即RISC 机),RISC机中采用的微处理器统称RISC处理器。这样一来,它能够以更快的速度执行操作(每秒执行更多百万条指令,即MIPS)。因为计算机执行每个指令类型都需要额外的晶体管和电路元件,计算机指令集越大就会使微处理器更复杂,执行操作也会更慢。
---------------------------------------------------------------------------
对 C 编程的影响
参考下例 (编译运行于 32 位 x86 机器,小字节序):
#include
typedef struct
{
union
{
unsigned char str[3];
unsigned long a;
unsigned short b;
};
} test;
int main(int argc, char * argv[]) {
astic cable tie, and banding material colors shoulddegree angle. 6.4.5 harness banding material for pl-beam with small harness line must be smooth transition, small wire harness tied to main beam should be at a 90-regularly, neat in appearance, the main6
test m;
m.a = 0x002233ff;
printf("%p %p %p\n", m.str, &m.a, &m.b);
printf("0x%x\n", m.b);
printf("%u %u\n", m.str[0], m.str[3]);
return 0;
}
输出结果为:(其中第一行的值可能会变化,但不管怎么变,三个值肯定是相同的) 0xbfd4d1d0 0xbfd4d1d0 0xbfd4d1d0
0x33ff
255 0
al for plastic cable tie, and banding material colors shoulddegree angle. 6.4.5 harness banding materi-beam with small harness line must be smooth transition, small wire harness tied to main beam should be at a 90-regularly, neat in appearance, the main7