当前位置:网站首页>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!
边栏推荐
猜你喜欢

12 【网页布局总结 元素的显示与隐藏】

剑指offer基础版 ---- 第26天

Distributed transaction processing solution big PK!

10 【组件编码流程 组件自定义事件 全局事件总线】

剑指offer基础版 ----第31天

数字孪生将成为进入“元宇宙”一项重要的途径

对于输出点是时间戳的渗透测试方法(以Oracle数据库为例)

16 【打包上线 图片懒加载】

Redis:安装使用

The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
随机推荐
剑指offer基础版--- 第23天
C语言教程(三)-if和循环
gin框架学习-JWT认证
局部变量成员变量、引用类型、this,static(第五天)
字符串的扩展
Object Detection Study Notes
数据库上机实验4 数据更新和视图
数字取证autopsy工具用法
Redis管道技术/分区
gin框架学习-Gin框架和Gorm框架搭建一个简单的API微服务
leetcode-每日一题558. 四叉树交集(分治递归)
Sword Point Offer Special Assault Edition ---- Day 2
leetcode-每日一题1217. 玩筹码(贪心+位运算)
字符串的新增方法
剑指offer基础版 ----第31天
leetcode-每日一题剑指 Offer II 041. 滑动窗口的平均值(队列模拟)
GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?
Proteus 8 Professional安装教程
了解SSRF,这一篇就足够了
C语言实验四 循环结构程序设计(一)