Chinaunix首页 | 论坛 | 博客
  • 博客访问: 180332
  • 博文数量: 33
  • 博客积分: 2047
  • 博客等级: 大尉
  • 技术积分: 333
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-21 19:35
文章分类

全部博文(33)

文章存档

2011年(1)

2010年(21)

2009年(11)

分类: C/C++

2009-11-21 21:16:52

主要测试处理器存放数据的大小端,输出你的平台是大端还是小端。
主要来自http://blog.chinaunix.net/u/18584/showart_512195.html

// 测试平台大小端
#include

typedef unsigned int UINT;

typedef union Endian_t
{
    int icount;
    char ch;
}Endian;

int main(int argc, char* argv[])
{
    printf("\n");
    Endian endDemo;
    endDemo.icount = 0x0A0B0C0D;    // 4B
    if(endDemo.ch == 0x0D) {
        printf("Little Endian: Low Byte store at Low memory address.\n");
    }
    else {
        printf("Big Endian: Low Byte store at High memory address.\n");
    }
    
    char *pByte = (char*)&endDemo;
    int i = 0;
    printf("------------------ Bytes In endDemo and their address -------------------\n");
    printf("Content: 0x%08x\n", endDemo.icount);
    for (i = 0; i < 4; ++i) {
        printf("pByte and *pByte: 0x%08x--->0x%02x\n", (UINT)pByte, (UINT)*pByte);
        ++pByte;
    }
    
    printf("\n");
    return 0;
}

makefile:
# This makefile is just an example.
# The following lines indicate how run-endian depends
# endian-demo.c, and how to create run-endian.out

# Define macros to reuse it.
CC= gcc
CFLAGS = -D_DEBUG -g

run-endian: endian-demo.c
    $(CC) $(CFLAGS) endian-demo.c -o run-endian.out

# clean target
# Clean all object-files
clean:
    rm -f *.out

我的平台unbuntu9.0.4 gcc4.3.3,得到的结果如下:
Little Endian: Low Byte store at Low memory address.
------------------ Bytes In endDemo and their address -------------------
Content: 0x0a0b0c0d
pByte and *pByte: 0xbf877d50--->0x0d
pByte and *pByte: 0xbf877d51--->0x0c
pByte and *pByte: 0xbf877d52--->0x0b
pByte and *pByte: 0xbf877d53--->0x0a
这说明我的686CPU是小端存放数据的,小端就是数据的低位字节存放在低地址处;大端就是
数据的低位字节存放在高地址处。程序结果说明很清楚。
CUBLog是个不错的地方,给了我好多优秀的资源,希望有更快的方法能找到优秀资源,让人类在网络上更有效的交流!
阅读(676) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~