当前位置:网站首页>详解判断大小端的方法
详解判断大小端的方法
2022-06-23 10:20:00 【stone_322】
1.大小端
小端存储:数据低字节保存在低地址中,高字节保存在高地址中。
大端存储:数据低字节保存在高地址中,高字节保存在低地址中。
2.判断方法
这里给出一种常见的方法,代码如下:
#include <stdio.h>
void test1(void)
{
union T
{
int a;
char b;
}t;
t.a = 1;
if (t.b == 1)
{
printf("小端\n");
}
else
{
printf("大端\n");
}
}
int main(void)
{
test1();
}
3.判断方法详解
为什么这种方法可以判断大小端呢?
首先,C语言中,联合体有固定存放顺序,一定是从低地址开始存放数据(①),知道了这一点,再结合字节对齐的知识,我们就可以判断出,联合体t在内存中应该是下图左边这样的(a和b实际共用一段内存,这里分开画是为了看得更清楚):
①处的结论,我们可以用下面的程序来验证:
#include <stdio.h>
void test2(void)
{
union T
{
int a;
char b;
}t;
void* p;
t.a = 1;
p = &t;
printf("&t = %p\n", p);
p = &t.b;
printf("&t.b = %p\n", p);
}
int main(void)
{
test2();
}
运行程序,可以看到&t和&t.b是一样的.
接着,判断大小端,我们可以在test2()的基础上,修改一下程序,得到:
void test3(void)
{
union T
{
int a;
char b;
}t;
char* p;
t.a = 1;
p = (char*)&t.a;
printf("%d\n", *p);
printf("&t.a = %p\n", p);
p = (char*)&t.a + 1;
printf("%d\n", *p);
p = (char*)&t.a + 2;
printf("%d\n", *p);
p = (char*)&t.a + 3;
printf("%d\n", *p);
p = (char*)&t.a - 1;
printf("%d\n", *p);
p = (char*)&t.a - 2;
printf("%d\n", *p);
p = (char*)&t.a - 3;
printf("%d\n", *p);
p = (char*)&t.a - 4;
printf("%d\n", *p);
p = (char*)&t.b;
printf("%d\n", *p);
printf("&t.b = %p\n", p);
}
运行程序,得到:
1
&t.a = 0024F62C
0
0
0
-52
64
49
64
1
&t.b = 0024F62C
所以可以得到结构体t的数据存储情况:
由此可见,t.a的低位1放在了低地址里,所以应该是小端存储。
边栏推荐
- Data structures and differences between MySQL InnoDB engine and MyISAM
- Picture storage -- Reference
- NOI OJ 1.2 10:Hello, World!的大小 C语言
- 2021-05-11 instanceof and type conversion
- Opencloudos uses snap to install NET 6
- Stockage d'images - référence
- Comic | code review is driving me crazy!
- 圖片存儲--引用
- Build a QQ robot to wake up your girlfriend
- Noi OJ 1.2 conversion between integer and Boolean C language
猜你喜欢

Stockage d'images - référence

Set up a QQ robot for ordering songs, and watch beautiful women

Liujinhai, architect of zhongang Mining: lithium battery opens up a Xintiandi for fluorine chemical industry

Unity technical manual - limit velocity over lifetime sub module and inherit velocity sub module

马斯克 18 岁儿子请愿改名,欲断绝父子关系

Mathematical analysis_ Notes_ Chapter 2: real and plural numbers

R and rstudio download and install detailed steps

文件IO(1)

MySQL基础-笔记

IPv6 的速度比 IPv4 更快?
随机推荐
Mysql 的Innodb引擎和Myisam数据结构和区别
How to solve the problem that easycvr does not display the interface when RTMP streaming is used?
Developer, you may have some misunderstandings about cloud computing
laravel8 beanstalk 使用说明
图片存储--引用
Is IPv6 faster than IPv4?
NOI OJ 1.3 20:计算2的幂 C语言
Confessing with Snake games (with source code)
NOI OJ 1.3 14:大象喝水 C语言
R和RStudio下载安装详细步骤
Successful experience of postgraduate entrance examination in materials and Chemical Engineering (metal) of Beijing University of Aeronautics and Astronautics in 2023
Tencent tangdaosheng: practice "science and technology for the good" and promote sustainable social value innovation
2022高考季征文获奖名单公布
2021-05-11instanceof和类型转换
JVM简单入门-02
2021-05-11static关键字
Google Earth Engine(GEE)——用不同方法计算slope对比案例分析
Noi OJ 1.2 integer data type storage space size
MySQL-01.工作中数据库优化和explain字段知识的总结
thymeleaf如何取url中请求参数值?