内存的大小端识别

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

所谓的大端模式比如说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 }

 

 

转载于:https://www.cnblogs.com/AC-LG/archive/2012/06/29/2570113.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值