当前位置:网站首页>About data storage in memory
About data storage in memory
2022-06-25 13:10:00 【LIn_ jt】
About the storage of data in memory
This article will explain the knowledge of floating-point numbers stored in memory
First , Let's take a look at the following program :>

While studying , The answer I give is very simple 9 9.0 9 9.0, However , When the program runs :>

To analyze the above procedures , First of all, we have to understand the storage of floating-point numbers in memory .
For binary floating point numbers , According to international standards IEEE( Institute of electrical and Electronic Engineering ) 754, Any binary floating point number V Can be expressed in the following form :>
*(-1) ^S * M 2 ^E
among (-1)^S Symbol bit , When s = 0 when ,V Is a positive number , When floating point number s = 1 , V It's a negative number .
M Is the significant bit , M>=1 And M < 2
2 ^E The power represents the exponential bit
for instance :>
For example, decimal floating point numbers 5.5, When we convert it to binary, it is :>
101.1 // Here is .1 The reason is here 1 The weight of is 2 ^-1 Power , That is to say 0.5. And we use it V The format of , Can write like this :>
*(-1)^0 1.011 * 2 ^ 2
namely S = 0; M = 1.011; E = 2;
about -5.5, Use it V The format of , I can express it as
*(-1)^1 (1.011) * 2 ^2;
For the storage of floating-point numbers in memory , There are the following rules
For single precision floating point numbers

For double precision floating point numbers :>

There are also some provisions : because M Always >=1 Less than 2, namely M Always equal to 1 The integer of , therefore , When stored in memory , Only decimal places are saved , example 5.5
It can be expressed as **(-1)^0 1.011 * 2 ^ 2*, M = 1.011. But what we store , It only stores 011, And when we use it , That's what makes 1 Make up for it .
For index E for , The first is a unsigned int Number of types , So its scope is 0 ~ 255(8bit) perhaps 0 ~2047(11bit)
When expressed in scientific counting , It is inevitable that E negative for example Decimal system 0.5 Expressed as a binary number :>
1*10 ^-1 also E The range of is greater than or equal to 0, therefore , Specify storage in memory E The true value of must be added with an intermediate number , The add 127 (8bit) or 1023(11bit) after , Then store it in memory
For example, just now -1., Save as 32 When floating-point numbers are in place , Save as -1 + 127 = 126 namely
01111110
Besides , Index E There are also the following provisions :>
if E The value of is not all 1 or 0, be E The real value of is the value stored in memory minus 127( or 1023), And then the significant number M Add the first one before .
For example, just now 0.5 Binary system V Expressed as :>
1.0*2 ^ -1 here E by -1, M by 1.0, Therefore, the data stored in memory is
0 01111110 00000000000000000000000
if E The data stored in is full 0 when , So let's think about it , What a small number it would be !, namely 2^ -127 Power , therefore , The regulations are as follows :>
When E The data stored in is full 0 when , Its index E The real value of is 1 - 127( or 1023), And the significant number M No more one before , But add 0, To represent a close 0 Number of numbers .
When E The data stored in is all data , that E The real value of is 128(8bit), that 2^E Power is a very large number , therefore ,V Expressed as positive and negative infinity .
Now let's go back to the first example at the beginning :>
#include <stdio.h>
int main()
{
int n = 9;
float* pFloat = (float*)&n;
printf("n The value of is :%d\n", n);
printf("*pFloat The value of is :%f\n", *pFloat);
*pFloat = 9.0;
printf("num The value of is :%d\n", n);
printf("*pFloat The value of is :%f\n", *pFloat);
return 0;
}

Take another look at the second part :>

Convert... As a floating point number n Changed to 9.0, that 9.0 The binary representation of :>
1001.0 namely 1.001*2^3 that S = 0; E = 3 + 127 = 130; M = 0.001. namely
0 1000 0010 00100000000000000000000
So in the form of integers , The data is :>
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Lw2C6Cbn-1638165164585)(C:\Users\MSI-NB\AppData\Roaming\Typora\typora-user-images\image-20211129090318437.png)]](/img/80/a182c36b6a88835bfbf0b336075649.png)
In the form of floating point numbers , Certainly 9.0 了 .
边栏推荐
- 出手即不凡,这很 Oracle!
- Command line garbled
- Back test of quantitative trading - example of futures CTA strategy (tqzfuturerenkoscalpingstrategy)
- Elemtnui select control combined with tree control to realize user-defined search method
- Lexical trap
- Summary of leetcode linked list problem solving skills
- [flask tutorial] flask development foundation and introduction
- 德国举行全球粮食安全团结会议
- WIN10环境下配置pytorch
- Summer Ending
猜你喜欢

Koa frame

Sword finger offer day 3 string (simple)

New Gospel of drug design: Tencent, together with China University of science and technology and Zhejiang University, developed an adaptive graph learning method to predict molecular interactions and

Sword finger offer day 2 linked list (simple)

Koa 框架

Fedora 35 部署DNS主从和分离解析 —— 筑梦之路

.NET in China - What's New in .NET

又是被Visdom搞崩溃的一夜

剑指offer 第 3 天字符串(简单)

Django framework - caching, signaling, cross site request forgery, cross domain issues, cookie session token
随机推荐
Sword finger offer II 028 Flatten multi-level bidirectional linked list
Resolved: could not find artifact XXX
J2EE from entry to earth 01 MySQL installation
Summer Ending
Summary of leetcode linked list problem solving skills
词法陷阱(C)
JSTL tag: fmt:formatdate tag format Chinese standard time or timestamp
PPT绘图之AI助力论文图
模块五(微博评论)
坡道带来的困惑
Sword finger offer II 025 Adding two numbers in a linked list
Reload cuda/cudnn/pytorch
C # switch between Chinese and English input methods
Update PIP & Download jupyter Lab
Resolution of PPT paper drawing
美创入选“2022 CCIA中国网络安全竞争力50强”榜单
Talk about 11 key techniques of high availability
量化交易之回测篇 - 期货CTA策略实例(TQZFutureRenkoScalpingStrategy)
Fedora 35 部署DNS主从和分离解析 —— 筑梦之路
与生产环境中的 console.log 说再见