<span style="font-family: Arial; font-size: 16px; line-height: 26px;"></span><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">端模式(Endian)的这个词出自Jonathan Swift书写的《格列佛游记》。这本书根据将鸡蛋敲开的方法不同将所有的人分为两类,<strong>从圆头开始将鸡蛋敲开的人被归为Big Endian,从尖头开始将鸡蛋敲开的人被归为Littile Endian(这句话最为形象)。</strong>小人国的内战就源于吃鸡蛋时是究竟从大头(Big-Endian)敲开还是从小头(Little-Endian)敲开。在计算机业Big Endian和Little Endian也几乎引起一场战争。在计算机业界,Endian表示数据在存储器中的存放顺序。下文举例说明在计算机中大小端模式的区别。</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">如果将一个32位的整数0x12345678存放到一个整型变量(int)中,这个整型变量采用大端或者小端模式在内存中的存储由下表所示。为简单起见,本文使用OP0表示一个32位数据的最高字节MSB(Most Significant Byte),使用OP3表示一个32位数据最低字节LSB(Least Significant Byte)。 </p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </p><table border="1" cellpadding="0" cellspacing="0" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><tbody><tr><td valign="top" width="189"><p>地址偏移</p></td><td valign="top" width="189"><p>大端模式</p></td><td valign="top" width="189"><p>小端模式</p></td></tr><tr><td valign="top" width="189"><p>0x00</p></td><td valign="top" width="189"><p>12(OP0)</p></td><td valign="top" width="189"><p>78(OP3)</p></td></tr><tr><td valign="top" width="189"><p>0x01</p></td><td valign="top" width="189"><p>34(OP1)</p></td><td valign="top" width="189"><p>56(OP2)</p></td></tr><tr><td valign="top" width="189"><p>0x02</p></td><td valign="top" width="189"><p>56(OP2)</p></td><td valign="top" width="189"><p>34(OP1)</p></td></tr><tr><td valign="top" width="189"><p>0x03</p></td><td valign="top" width="189"><p>78(OP3)</p></td><td valign="top" width="189"><p>12(OP0)</p></td></tr></tbody></table><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>小端:较高的有效字节存放在较高的的存储器地址,较低的有效字节存放在较低的存储器地址。
大端:较高的有效字节存放在较低的存储器地址,较低的有效字节存放在较高的存储器地址。
</strong>如果将一个16位的整数0x1234存放到一个短整型变量(short)中。这个短整型变量在内存中的存储在大小端模式由下表所示。 </p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </p><table border="1" cellpadding="0" cellspacing="0" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><tbody><tr><td valign="top" width="189"><p>地址偏移</p></td><td valign="top" width="189"><p>大端模式</p></td><td valign="top" width="189"><p>小端模式</p></td></tr><tr><td valign="top" width="189"><p>0x00</p></td><td valign="top" width="189"><p>12(OP0)</p></td><td valign="top" width="189"><p>34(OP1)</p></td></tr><tr><td valign="top" width="189"><p>0x01</p></td><td valign="top" width="189"><p>34(OP1)</p></td><td valign="top" width="189"><p>12(OP0)</p></td></tr></tbody></table><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">由上表所知,采用大小模式对数据进行存放的主要区别在于在存放的字节顺序,大端方式将高位存放在低地址,小端方式将高位存放在高地址。采用大端方式进行数据存放符合人类的正常思维,而采用小端方式进行数据存放利于计算机处理。到目前为止,采用大端或者小端进行数据存放,其孰优孰劣也没有定论。</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">有的处理器系统采用了小端方式进行数据存放,如Intel的奔腾。有的处理器系统采用了大端方式进行数据存放,如IBM半导体和Freescale的PowerPC处理器。不仅对于处理器,一些外设的设计中也存在着使用大端或者小端进行数据存放的选择。</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">因此在一个处理器系统中,有可能存在大端和小端模式同时存在的现象。这一现象为系统的软硬件设计带来了不小的麻烦,这要求系统设计工程师,必须深入理解大端和小端模式的差别。大端与小端模式的差别体现在一个处理器的寄存器,指令集,系统总线等各个层次中。</p>
<span style="font-family: Arial; font-size: 16px; line-height: 26px;">
</span>
<span style="font-family: Arial; font-size: 16px; line-height: 26px;">
</span>
<span style="font-family: Arial; font-size: 16px; line-height: 26px;">C语言简洁的实现就要用到联合体(union),简单的说union就是一种结构,在union中所有的数据成员共用一个存储空间,在同一时间只能存储其中的一个数据成员,所有的数据成员具相同的起始地址,相对于基地址的偏移量都为0;</span>
int CheckEndian(void)
{
union check
{
int Word;
char Half;
} Endian;
Endian.Word=1;
if(1 == Endian.Half)
return Little_Endian;
else
return Big_Endian;
}