当前位置:网站首页>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!
边栏推荐
猜你喜欢
随机推荐
账号或密码多次输入错误,进行账号封禁
实验8 DNS解析
可以“繁殖”的程序
wpf wrapPanel居中并从左到右排列
数据库上机实验4 数据更新和视图
第7章 网络层第2次练习题答案(第三版)
vulhub靶场学习日记hackme1
SQL注入中数据库的判断
【JVM加载】---类加载机制
Error: Cannot find module ‘D:\Application\nodejs\node_modules\npm\bin\npm-cli.js‘
uni-app进阶之创建组件/原生渲染【day9】
字符串的新增方法
初识正则表达式
元宇宙的前景及四大赛道
gin框架学习-Casbin入门指南(ACL、RBAC、域内RBAC模型)
数据库上机实验7 数据库设计
了解SSRF,这一篇就足够了
The interviewer asked me TCP three handshake and four wave, I really
Swordsman Offer Special Assault Edition --- Day 3
uni-app进阶之模版语法与数据绑定【day7】