woaidongmao

文章均收录自他人博客,但不喜标题前加-[转贴],因其丑陋,见谅!~
随笔 - 1469, 文章 - 0, 评论 - 661, 引用 - 0
数据加载中……

判断big endian和little endian的方法

Big endian machine: It thinks the first byte it reads is the biggest.

Little endian machine: It thinks the first byte it reads is the littlest.

#include <stdio.h>

int main(int argc, char **argv)
{
        union {
          short  s;
      char   c[sizeof(short)];
    } un;

        un.s = 0x0102;
        if (sizeof(short) == 2) {
                if (un.c[0] == 1 && un.c[1] == 2)
                        printf("big-endian\n");
                else if (un.c[0] == 2 && un.c[1] == 1)
                        printf("little-endian\n");
                else
                        printf("unknown\n");
        } else
                printf("sizeof(short) = %d\n", sizeof(short));

        exit(0);
}

总结一下:big endian是适合大家的阅读顺序,little endian则相反。

 

 

 

/usr/include/中(包括子目录)查找字符串BYTE_ORDER(或_BYTE_ORDER, __BYTE_ORDER),确定其值。这个值一般在endian.h或machine/endian.h文件中可以找到,有时在feature.h中,不同的操作系统可能有所不同。一般来说,Little Endian系统BYTE_ORDER(或_BYTE_ORDER,__BYTE_ORDER)为1234,Big Endian系统为4321。大部分用户的操作系统(如windows, FreeBsd,Linux)是Little Endian的。少部分,如MAC OS ,是Big Endian 的。本质上说,Little Endian还是Big Endian与操作系统和芯片类型都有关系。

Processor OS Order 
x86 (Intel, AMD, … ) All little-endian 
DEC Alpha All little-endian 
HP-PA NT little-endian 
HP-PA UNIX big-endian 
SUN SPARC All? big-endian 
MIPS NT little-endian 
MIPS UNIX big-endian 
PowerPC NT little-endian 
PowerPC non-NT big-endian 
RS/6000 UNIX big-endian 
Motorola m68k All big-endian
 

posted on 2009-09-27 14:32 肥仔 阅读(1328) 评论(0)  编辑 收藏 引用 所属分类: C++ 基础


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理