Chinaunix首页 | 论坛 | 博客
  • 博客访问: 376139
  • 博文数量: 48
  • 博客积分: 743
  • 博客等级: 上士
  • 技术积分: 956
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-22 13:51
文章分类

全部博文(48)

文章存档

2015年(3)

2014年(17)

2012年(23)

2011年(5)

分类: C/C++

2011-12-21 17:01:17

计算机存储数据(如一个int型)时分为大端存储和小端存储两种方式;小端存储方式为低地址存储低字节数据,高地址存储高字节数据。大端存储方式相反,即:低地址存储高字节数据,高地址存储低字节数据。X86下存储方式为小端存储,Power PC为大端存储(ARM也可设置为大端存储)。
具体存储方式示意图如下:(小端存储方式示意)


低地址存储底字节数据,高地址存储低字节数据
测试方法:1.定义一个包含int 和 char到联合体union   2.利用char*型到指针
方法一代码:
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. int main(int argc ,char *argv[])
  4. {
  5.     union name{
  6.         int i;
  7.         char a;
  8.     }c;
  9.     c.i=0x04030201;

  10.     printf("%d\n",c.a);//输出4为大端存储,输出1为小端存储
  11.     return 0;
  12. }
方法二代码:
  1. #include<stdio.h>
  2. #include<stdlib.h>


  3. int main(int argc ,char *argv[])
  4. {
  5.     int num = 0x04030201;
  6.     char *p = (char*)&num;

  7.     printf("address is %p,count is %d\n",p,*p);//输出4为大端存储,输出1为小端存储
  8.     printf("address is %p,count is %d\n",p+1,*(p+1));
  9.     printf("address is %p,count is %d\n",p+2,*(p+2));
  10.     printf("address is %p,count is %d\n",p+3,*(p+3));
  11.     return 0;
  12. }



阅读(2996) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~