机器字节存储顺序

 

 

big endian

[zt]

(1)http://hi.baidu.com/cppyun/blog/item/9625c8396d5ff7f33b87ce33.html

8086机器都是使用little endian, 而摩托罗拉的power pc使用big endian
对于一个数0x1122
产用little endian方式时  低字节存储0x22,高字节存储0x11.
而使用big endian方式时, 低字节存储0x11, 高字节存储0x22
在这俩种字节方式间转换可以使用汇编指令 BSWAP

测试函数

int IsMyMachineBigEndian()
{
       unsigned short test = 0x1122;
       unsigned char  *cp = (unsigned char *)&test;
       /*printf("/n %x",*cp++);
       printf("/n %x",*cp++);*/

       return (*cp == 0x11);
}


若返回真值,则说明是big endian

PC结果:

     22

     11

(2)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.

int IsMyMachineBigEndian()
{
       unsigned short test = 0x1122;
       unsigned char  *cp = (unsigned char *)&test;
       /*printf("/n %x",*cp++);
       printf("/n %x",*cp++);*/

       return (*cp == 0x11);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值