当前位置:网站首页>C language from entry to such as soil, the data store
C language from entry to such as soil, the data store
2022-07-31 03:37:00 【bubble milk】
个人主页:泡泡牛奶
系列专栏:C语言从入门到入土
This content will give you a specific understanding of the shape(源码、反码、补码)、浮点型(IEEE 754标准)在内存中的存储,特别实用,一看就会,走过路过千万不要错过( •̀ ω •́ )*
Let's take a look at today's contento(〃^▽^〃)o
1. 数据类型的介绍
In the previous chapters, we basically recognized various data types,Let's recall a little here
类型的意义:
- 决定to visit内存空间的大小
- Determines the perspective from which to look at the memory space(例如:Integer and character data types)
1.1 类型的基本归类
整型家族:
char
unsigned char
signed char
short
unsigned short [int]
signed short [int]
int
unsigned int
signed int
long
unsigned long [int]
signed long [int]
浮点型家族:
float
double
构造类型:
> 数组类型
> 结构体类型 struct
> 枚举类型 enum
> 联合类型 union
指针类型:
int *pi;
char *pc;
float *pf;
void *pv;(泛型指针)
空类型:
void 表示空类型 (无类型)
通常用于 函数的返回类型、函数的参数、指针类型
//作为函数的返回类型
void f1()//No return value is accepted
{
;
}
//作为函数的参数
int f2(void)//参数为void时,传入参数会报错
{
;
}
//as a pointer type
void* f3(void* pv)//参数为 void* 时,可接收任意类型的参数
{
;
}
2. 整型在内存中的存储
计算机中的整数有三种2进制表示方法,即源码、反码、补码.
三种表示方法均有符号位和数值位
符号位: " 0 " 表示 正," 1 "表示 负
数值位:
正数:
原码、反码、补码相同
负数:
原码:Directly translate the value to binary
反码:原码符号位不变,其它位按位取反
补码:反码+1
We can see in memory storage:
这里大家可能有个疑问,为什么j
和i
In memory it would be like this?为什么i
和 j
内部Binary will倒过来存储呢??编译器坏了?(bushi
If you are curious about this,就接着往下看吧.
2.1 什么是大小端?
大小端存储是计算机的一种存储方式,The way its primary storage is also determined by the computer.
什么是大小端存储:
大端(存储)模式:指的是,数据的低 (二进制)位保存在高地址中,高位保存在低地址中;
小端(存储)模式:指的是,数据的低位保存在低地址中,高位保存在高地址中;
注意:
Big-endian storage only followsA single type of data的存储方式有关,The storage of the array is still from low address to high address
3. 浮点数在内存中的存储
Before recognizing the storage of floating point numbers in memory,我们先看一下下面的例子:
int main()
{
int n = 9;
float* pf = (float*)&n;
printf("n的值为:%d\n", n);
printf("pf的值为:%f\n", *pf);
*pf = 9.0;
printf("n的值为:%d\n", n);
printf("pf的值为:%f\n", *pf);
return 0;
}
Why is the output like this?
To understand the reason, we must first know the storage rules of floating-point numbers in memory
3.1 浮点数在内存中的存储规则
Floating point numbers are stored according to international standards IEEE (电气工程师学会) 754 来进行存储,具体规则如下:
- (-1)^S * M * 2^E
- (-1)^S 代表 符号位,S=0为正数,S=1为负数
- M 表示有效数字,(1 ≤ M < 2)
- 2^E表示指数位
IEEE 754规定:
对于 float
类型来说:
对于 double
类型来说:
IEEE 754对 有效数字M 和 指数E 有一些特殊的规定:
有效数字M
前面说过,
1 ≤ M < 2
,也就是说,M可以写成1.xxxxxx
的形式,将 Omit before the decimal point,则其中xxxxxx
That's the number we want to save
指数E
对于
float
类型(E 占 8 位):E + 127
(取 0~255 中间数)对于
double
类型(E 占 11 位):E + 1023
(取 0~2047 中间数)
举个例子(o゚v゚)ノ
十进制的
5.0
want to save it
- 小数点前 需要转换成 二进制
101 . 0
- Convert it according to scientific notation,
1.01 x 2^(2)
- 指数E 进行替换
E = 2+127 = 129 = 1000 0001(二进制)
- 由上可得出
S = 0
E = 1000 0001
M = 1.01
0 10000001 01000000000000000000000
S E M
整理一下:
0100 0000 1010 0000 0000 0000 0000 0000
用十六进制表示:
4 0 a 0 0 0 0 0
整理一下:(大端存储)
40 a0 00 00
小端存储:
00 00 a0 40
While it's still warm,Check it out soon(/≧▽≦)/
十进制
-2.25
在内存中如何存储?
- 符号为负
S = 1
- Binary is being written before the decimal point,Follow after the decimal point
2^(-n)
,n = 4 (1/4=0.25),(Write binary backwards),所以二进制为10.01
- Expressed in scientific notation
1.001 x 2^(1)
- 指数E进行替换
E = 1 + 127 = 128 = 1000 0000(二进制)
- 由上可得
S = 1
E = 1000 0000
M = 1.001
1 10000000 00100000000000000000000
S E M
整理一下:
1100 0000 0001 0000 0000 0000 0000 0000
用十六进制表示:
c 0 1 0 0 0 0 0
整理一下:
c0 10 00 00
小端存储:
00 00 10 c0
注意:(特殊情况)
当指数Ewhen the boundary value 0~255(0~1023)directly equal to the concrete true value
E全为0:
- 直接等于0
E全为1:
- directly equal to the specific value340282346638528859811704183484516925440.000000(2^255)
好啦ヾ(≧▽≦*)o 本期的内容到这里就全部结束了,If it helps you, don't forget to give a big one三连支持一下博主(o゚v゚)ノ
边栏推荐
- Daily practice of LeetCode - 138. Copy a linked list with random pointers
- 5. How does the SAP ABAP OData service support the $filter operation
- The Map Entry understanding and application
- 进程间通信
- 慧通编程第4关 - 魔法学院第6课
- A brief introduction to the CheckBox component of the basic components of Flutter
- 数据库实现分布式锁
- Redis 统计用户新增和留存
- Detailed explanation of TCP (1)
- 想从手工测试转岗自动化测试,需要学习哪些技能?
猜你喜欢
[Swift]自定义点击APP图标弹出的快捷方式
数据库实现分布式锁
Safety 20220712
BP神经网络
[Swift] Customize the shortcut that pops up by clicking the APP icon
"DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction" paper notes
TCP和UDP详解
(八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类
「 每日一练,快乐水题 」1331. 数组序号转换
How Zotero removes auto-generated tags
随机推荐
IDEA comment report red solution
Component pass value provide/inject
Pytest e-commerce project combat (on)
数据库文件中的未分配的块和未使用的块的区别
[Dynamic programming] Maximum sum of consecutive subarrays
RESTful api接口设计规范
What skills do I need to learn to move from manual testing to automated testing?
[Compilation principle] Design principle and implementation of recursive descent parsing
SocialFi 何以成就 Web3 去中心化社交未来
Postgresql 15 source code analysis (5) - pg_control
Why don't you programmers make a living off your own projects?And have to work for someone else?
Based on the local, linking the world | Schneider Electric "Industrial SI Alliance" joins hands with partners to go to the future industry
分布式锁以及实现方式三种
Annotation usage meaning
Day32 LeetCode
The use of beforeDestroy and destroyed
BUG definition of SonarQube
安全20220712
浅识Flutter 基本组件之CheckBox组件
「 每日一练,快乐水题 」1331. 数组序号转换