当前位置:网站首页>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!
边栏推荐
猜你喜欢
随机推荐
数据库上机实验1 数据库定义语言
Memcached :安装
剑指offer基础版--- 第23天
Access数据库的查询
C语言实验二 数据类型、运算符和表达式
剑指offer专项突击版 --- 第 3 天
1D, 2D, 3D convolution operations in pytorch
详解扫雷游戏(C语言)
Error: Cannot find module ‘D:\Application\nodejs\node_modules\npm\bin\npm-cli.js‘
The process and specific code of sending SMS verification code using flask framework
gin框架学习-Casbin入门指南(ACL、RBAC、域内RBAC模型)
C语言教程(三)-if和循环
The interviewer asked me TCP three handshake and four wave, I really
数据库上机实验3 连接查询和分组查询
什么是 GameFi?
剑指offer基础版 --- 第22天
Swordsman Offer Special Assault Edition ---- Day 6
Qt Creator + CMake 运行调试总会自动 build 所有目标
剑指offer专项突击版 ---第 5 天
C语言实验三 选择结构程序设计