当前位置:网站首页>判断大小端存储两种办法
判断大小端存储两种办法
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");
}边栏推荐
- BZOJ3189 : [Coci2011] Slika
- [Part 13] source code analysis and application details of completabilefuture class [key]
- In the post epidemic era, how can enterprise CIOs improve enterprise production efficiency through distance
- Experiment 10 Bezier curve generation - experiment improvement - interactive generation of B-spline curve
- 即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍
- JVM | virtual machine stack (local variable table; operand stack; dynamic link; method binding mechanism; method call; method return address)
- Redis basic data type (Zset) ordered collection
- JVM class loader; Parental delegation mechanism
- [Part 16] copyonwritearraylist source code analysis and application details [key]
- JVM | runtime data area; Program counter (PC register);
猜你喜欢

类和对象(1)

类和对象(2)

LeetCode-104-二叉树的最大深度

Apache local multi port configuration

zypper命令使用示例

Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience

LeetCode-98-验证二叉搜索树

How does the chief financial officer of RPA find the "super entrance" of digital transformation?

Endnotex9 introduction and basic tutorial instructions

How to use RPA robot to start the first step of digital transformation of freight forwarding industry?
随机推荐
EndnoteX9簡介及基本教程使用說明
实现 TabLayout 下标与文字等长,选中字体大小改变
LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)
JVM class loader; Parental delegation mechanism
类和对象(4)
Rexroth overflow valve zdb6vp2-42/315v
Supplementary questions for the training ground on September 11, 2021
Customer information management software
Leetcode-322- change exchange
AC自动机
[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)
Codeforces Round #742 (Div. 2) F. One-Four Overload
Jenkins+allure integrated report construction
As a senior abap consultant, which SAP technology can be selected as the main direction in the next step?
即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍
Leetcode-110-balanced binary tree
CANN编码的一些报错汇编
Leetcode 797. All possible paths
RPA丨首席财务官如何找到数字化转型“超级入口”?
Educational Codeforces Round 114 (Rated for Div. 2) D