• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
AC_LG
博客园    首页    新随笔    联系   管理    订阅  订阅

内存的大小端识别

计算机的内存中,存在两种存储模式:大端模式,小端模式。

所谓的大端模式比如说int a = 1这个变量,一种有32位,4个字节,那么在内存中的存储应该是这样的

地址  00   01   02  03   (低到高)

 0x   00   00   00  01

小端模式就是反过来,小端地址放低地址的值

地址  00   01   02  03   (低到高)

 0x   01   00   00  00

 

那么如何用程序来识别这两种情况呢,下面有两种办法:

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 union test
 6 {
 7   char    b;
 8   int    c;
 9 };
10 
11 int main()
12 {
13     //the first method 
14     union test u;
15     u.c = 1;
16     
17     if((int)u.b == 1)
18             printf("little\n");
19     else
20             printf("big\n");
21     
22     //the second method    
23     int a = 1;
24     
25     if(*((char *)&a) == 1)
26         printf("little\n");
27     else
28         printf("big\n");
29     
30     system("pause");
31     return 0;
32 }

 

 

posted @ 2012-06-29 16:17  AC_LG  阅读(166)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3