当前位置:网站首页>How to distinguish big and small endian in C language
How to distinguish big and small endian in C language
2022-07-31 05:46:00 【How so many names are occupied】
Shocked!It is so easy to distinguish big and small end in C language
C language has an interview question called distinguishing whether a computer is big endian or little endian.
The big endian mode means that the high byte of the data is stored in the low address of the address space, and the low byte of the data is stored in the high address of the address space
Little-endian mode means that the high byte of the data is stored in the high address of the address space, and the low byte of the data is stored in the low address of the address space
How to tell?code above
#includeint main(void){short a=0x01;//0x.. is a hexadecimal number, 0x01 is written as a short type is 00000000 00000001char *c=&a;//When accessing the address in the variable, follow the access rules of char, if it is big endian, then *c=0if((*c)==0){printf("big\n");}else{printf("small\n");}return 0;}
??? Are you confused?I believe that people with a bit of basic knowledge must know this
#include typedef union Un{char c;short i;}un;int main(){un n;n.i=1;if (1 == n.c){printf("small\n");}else{printf("big\n");}return 0;}
This code should not need to be explained, there are a lot of them on the Internet.So, let me explain the code above.
As the comment says, the variable a is stored in the address space like this:
00000000 00000001
The address stored in the pointer type variable p is the address of a, but when reading with the char access rule, only read
00000000
or
00000001
One of.If the read is 00000000, it means that the high byte is stored in the low address of the address space, otherwise the low byte is stored in the low address of the address space.From this, you can judge the size end, is it very simple?
What else is there to do?Share it!
边栏推荐
- 继承、Super,重写、抽象类、抽象方法 1(第七天)
- (Crypto必备干货)详细分析目前NFT的几大交易市场
- The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
- tf.keras.utils.pad_sequences()
- 04 【计算属性 侦听属性】
- 碎片化NFT(Fractional NFT)
- leetcode-2321. 拼接数组的最大分数(差分+枚举)
- 11 【组件通信】
- Swordsman Offer Special Assault Edition ---- Day 6
- 踏上编程之路,你必须要干的几件事
猜你喜欢
随机推荐
数据库上机实验3 连接查询和分组查询
leetcode-每日一题558. 四叉树交集(分治递归)
【数据库学习】Redis 解析器&&单线程&&模型
vulhub靶场学习日记hackme1
Oracle数据库中的“limit”查询
正则表达式基础知识
碎片化NFT(Fractional NFT)
第7章 网络层第3次练习题答案(第三版)
gin框架学习-Casbin进阶之策略管理API使用方法
1D, 2D, 3D convolution operations in pytorch
[MQ I can speak for an hour]
04 【计算属性 侦听属性】
Proteus 8 Professional安装教程
剑指offer基础版 --- 第22天
Memcached :安装
leetcode-每日一题873. 最长的斐波那契子序列的长度(哈希和二分)
剑指offer基础版 ----- 第25天
剑指offer基础版--- 第23天
leetcode-每日一题565. 数组嵌套(标记图和并查集)
Redis:安装使用