当前位置:网站首页>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!
边栏推荐
猜你喜欢
随机推荐
什么是 GameFi?
vulhub靶场学习日记SickOs1.2
Flink sink redis writes to Redis
C语言教程(三)-if和循环
11 【定位】
代码块、Package,Import,封装(第六天)
16 【打包上线 图片懒加载】
C语言指针详解
基于web3.0使用钱包Metamask的三方登陆
Three-party login using wallet Metamask based on web3.0
实验7 UDP与TCP对比
【数据库学习】Redis 解析器&&单线程&&模型
leetcode-829. 连续整数求和(数论)
uni-app进阶之样式框架/生产环境【day10】
在kali上搭建vulhub漏洞靶场
利用phpstudy搭建DVWA
gin框架学习-Casbin入门指南(ACL、RBAC、域内RBAC模型)
数据库上机实验6 数据库完整性
First acquaintance with Flask
Object,多态 1(第八天)









