当前位置:网站首页>判断大小端存储两种办法
判断大小端存储两种办法
2022-06-11 21:36:00 【爱学代码的学生】
1. 强制转换
给定 int类型的变量a,赋值为1,1的16进制为:00 00 00 01。
若小端存储则a中存储为:01 00 00 00。
大端存储为:00 00 00 01。
则可以取出a的地址强转为char*类型,*(char*)&a来判断值为0/1。
如果是0则为大端存储,是1为小端存储。
代码实现如下:
int main()
{
int a = 1;
//00 00 00 01
if (*(char*)&a == 1)
printf("小端存储\n");
else
printf("大端存储\n");
return 0;
}2. 利用共用体
如图:

我们可以发现a,b的地址是相同的,那么对于b的改变,则也会改变a。因为b占有四个字节,而a则只有一个字节。

如果是小端存储则c中为1,
相反如果是大端存储则c中为0。
代码实现如下:
typedef union A
{
char a;
int b;
}A;
int main()
{
A a;
a.b = 1;
if (a.a == 1)
printf("小端存储\n");
else
printf("大端存储\n");
}边栏推荐
- JVM|本地方法接口;本地方法栈
- Experiment 10 Bezier curve generation - experiment improvement - interactive generation of B-spline curve
- Leetcode-104- maximum depth of binary tree
- Rexroth overflow valve zdb6vp2-42/315v
- AC自动机
- LeetCode-155-最小栈
- Relatively perfect singleton mode
- CANN编码的一些报错汇编
- JVM|前言介绍
- Some error reporting assemblies of cann code
猜你喜欢
随机推荐
为什么需要微服务
[Part 13] source code analysis and application details of completabilefuture class [key]
字符串复制函数
RPA+低代码助推品牌电商启新创变、重启增长
Builder pattern
Experiment 10 Bezier curve generation - experiment improvement - control point generation of B-spline curve
类与对象(3)
领先企业推进智慧财务的同款效率工具,赶快了解一下?
JVM|类加载器;双亲委派机制
Jenkins+allure integrated report construction
EndnoteX9简介及基本教程使用说明
关于斜率优化
「大模型」之所短,「知识图谱」之所长
Leetcode-110-balanced binary tree
Educational Codeforces Round 114 (Rated for Div. 2) D
Experiment 10 Bezier curve generation - experiment improvement - interactive generation of B-spline curve
即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍
The network connection is normal, but Baidu web page can not be opened and displayed. You can't access this website solution
如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称试读版
Usage of esp32c3 Arduino Library









