php 大端与小端,大端、小端的区别(Big-endian or Little-endian)

在不同的计算机体系结构中,对于数据(比特、字节、字等)的存储和传输机制有所不同;目前在各种体系结构的计算机中,主要采用的字节存储机制主要有两种:大端(Big-endian)和小端(Little-endian)。

字节序,又称端序,尾序,英文:Endianness。在计算机科学领域中,字节序是指存放多字节数据的字节(byte)的顺序,典型的情况是整数在内存中的存放方式和网络传输的传输顺序。Endianness有时候也可以用指位序(bit)。

“endian”一词来源于乔纳森·斯威夫特的小说格列佛游记。小说中,小人国为水煮蛋该从大的一端(Big-End)剥开还是小的一端(Little-End)剥开而争论,争论的双方分别被称为Big-endians和Little-endians。

1980年,Danny Cohen在其著名的论文"On Holy Wars and a Plea for Peace"中,为平息一场关于字节该以什么样的顺序传送的争论,而引用了该词。

首先有两个概念需要解释一下:

1.MSB(全大写)指the Most Significant Byte,指多字节序列中具有最大权重的字节。

2.LSB(全大写)指the Least Significant Byte,指多字节序列中具有最小权重的字节。

PS:小写的msb,lsb还有另外的意思,最高有效位(the Most Significant Bit,msb)和最低有效位(the least significant bit,lsb)。

大端(Big-endian):MSB放在低地址,LSB存放在高地址。(大端方式将高位存放在低地址)

小端(Little-endian):MSB存放在高地址,LSB存放在低地址。(小端方式将高位存放在高地址)

当然另外,还有一种混合序(mixed-endian or middle-endian),此处暂不解释了。

这个说的有点抽象,下面举个例子吧:

以一个数值为1029的整数(4个字节)为例子吧,其分别在大端和小端架构中的存储方式分别为下表所示:

Big-endian VS. Little-endian

----------------------------------------------------------------------------

Address

Big-endian

Little-endian

0 byte

00000000

00000101

1 byte

00000000

00000100

2 byte

00000100

00000000

3 byte

00000101

00000000

----------------------------------------------------------------------------

在主流的体系结构中,小端(Little-endian)的代表是我们最常用x86架构(包括x86_64),还有 6502 (including 65802, 65C816), Z80 (including Z180, eZ80 etc.), MCS-48, 8051, DEC Alpha, Altera Nios, Atmel AVR, SuperH, VAX, 和 PDP-11 等等;

大端(Big-endian)的架构有 Motorola 6800 and 68k, Xilinx Microblaze, IBM POWER, system/360, System/370 等等。

另外有一些架构的endian是可配置的,如下架构有配置endian为大端、小端中任一种的功能, ARM, PowerPC, Alpha, SPARC V9, MIPS, PA-RISC 和 IA-64 等等。

下面,这个简单的C程序,可以查询出当前的架构是大端还是小端。

Python

/*

* To check CPU is Little endian or Big endian.

* x86 is little-endian, many of other CPUs are big-endian.

* ARM is Bi-endian (configurable endian).

* Author: Jay Ren

* Date: 2012.01.04

*/

#include

int main(int argc, char *argv[])

{

int num = 1;

if(*(char*)&num == 1)

printf("Your CUP is Little Endian.\n");

else

printf("Your CUP is Big Endian.\n");

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/*

*TocheckCPUisLittleendianorBigendian.

*x86islittle-endian,manyofotherCPUsarebig-endian.

*ARMisBi-endian(configurableendian).

*Author:JayRen

*Date:2012.01.04

*/

#include

intmain(intargc,char*argv[])

{

intnum=1;

if(*(char*)&num==1)

printf("Your CUP is Little Endian.\n");

else

printf("Your CUP is Big Endian.\n");

}

参考:

http://en.wikipedia.org/wiki/Endianness

"Linux Kernel Development" book

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值