当前位置:网站首页>How are integer and floating-point types stored in memory
How are integer and floating-point types stored in memory
2022-07-03 15:23:00 【Little snail rushes towards】
Catalog
1.1 Basic classification of types :
2.2 Introduction of big and small endings
3 Floating point storage in memory
3.1 Floating point storage rules
Preface
Hello, everyone ! In this issue, I will share with you how integer and floating-point data are stored in memory .
1 data type
We already know the basic built-in types :
The meaning of type :
1. Use this type to exploit the size of memory space ( Size determines the range of use ).
2. How to look at the perspective of memory space .
1.1 Basic classification of types :
Plastic surgery Family :
char
unsigned char
signed char
short
unsigned short [int]
signed short [int]
int
unsigned int
signed int
long
unsigned long [int]
signed long [int]
char Depending on the compiler , stay VS In the compiler char yes signed char.
Floating point family :
float
double
Among them float Low type accuracy , Small storage range .
and double High type accuracy , Large storage range .
Construction type :
> An array type
> Type of structure struct
> Enumeration type enum
> Joint type union
Pointer types
int *pi;
char *pc;
float* pf;
void* pv;
Empty type :
void Indicates empty type ( No type )
Usually applied to the return type of a function 、 The parameters of the function 、 Pointer types .
What I want to share with you today is Plastic surgery Family and Floating point family How is it stored in memory .
2 Shaping storage in memory
2.1 Three forms of binary
Original code : Express directly in binary .
Inverse code : The sign bit of the original code remains unchanged , Other bits are reversed .
Complement code : Inverse code +1.
Be careful :
1 The original code of positive number 、 The inverse code is the same as the complement code . The binary transformation rules of negative numbers are as follows .
2 There are two ways to change the complement code into the original code :
Method 1
Method 2
So what form of binary is plastic stored in memory ?
In fact, shaping is stored in memory in the form of complement .
In computer system , All values are represented and stored by complements . The reason lies in , Use complement , You can combine sign bits and numeric fields One processing ;
meanwhile , Addition and subtraction can also be handled in a unified way (CPU Only adders ) Besides , Complement code and original code are converted to each other , Its operation process It's the same , No need for additional hardware circuits .
What is this ?
Actually, I just learned here , I'm also a little confused , Why use complement to save ? Let me give you an example
1-1 How is the result of this operation carried out
1 The original code of ( stay 32 A platform ):00000000000000000000000000000001
-1 The original code of ( stay 32 A platform ):10000000000000000000000000000001
-1 The inverse of ( stay 32 A platform ):111111111111111111111111111111111110
-1 Complement ( stay 32 A platform ):111111111111111111111111111111111111
because CPU Only adders , therefore 1-1---->1+(-1)
Obviously, whether it is 1 The original code of is still the inverse code and -1 The result cannot be obtained by adding the original code or complement of .
And the complement can 100000000000000000000000000000000( here 33), And we are 32 Bit platform , So the end result is 00000000000000000000000000000000(0, Is it amazing to see Jue's complement here . therefore : Integers store complements in memory .
Although we know that what is stored in memory is a complement , But we still don't know how the complement is stored in memory , Next we will talk about the big and small endings .
2.2 Introduction of big and small endings
Let's use integer 1 To understand the
Big end byte order storage
Is to put a data High byte order The content of is in Low address , Low byte order The content of is High address .
We use it 16 Hexadecimal represents distribution
Small end byte order storage
Is to put a data High byte order The content of is in High address , Low byte order The content of is Low address .
The following is the vs Storage distribution in
The storage of byte order at the big and small end is related to hardware , Some hardware is small end storage , There is big end storage .
3 Floating point storage in memory
Many people will think , Shaping is stored in memory in the form of complement , The storage mode depends on the size , So how is floating-point type stored ?
From the above, we may see that it is definitely not related to the storage method of shaping .
3.1 Floating point storage rules
According to international standards IEEE( Institute of electrical and Electronic Engineering ) 754, Any binary floating point number V It can be expressed in the following form :
(-1)^S * M * 2^E
(-1)^S The sign bit , When S=0,V Is a positive number ; When S=1,V It's a negative number .
M Represents a significant number , Greater than or equal to 1, Less than 2.
2^E Indicates the index bit .
that 1.0 I can write this as .(-1)^0* 1.0*2^0(2^n Power is the form of binary scientific calculation )
Two of them S = 0,M = 1.0,E = 0
that 9.5 We can write
(-1)^0*1.0011*2^3
and 9.6 Well ?
We find that we can never show 0.6, Can only keep approaching this number , So the storage of floating-point in memory is a The exact value Instead of a Accurate value .
IEEE 754 Regulations :
about 32 Floating point number of bits , The highest 1 Bits are sign bits s, And then 8 Bits are exponents E, The rest 23 Bits are significant numbers M.
about 64 Floating point number of bits , The highest 1 Bits are sign bits S, And then 11 Bits are exponents E, The rest 52 Bits are significant numbers M.
IEEE 754 For significant figures M And the index E, There are some special rules .
As I said before , 1≤M<2 , in other words ,M It can be written. 1.xxxxxx In the form of , among xxxxxx Represents the fractional part . IEEE 754 Regulations , Keep it in the computer M when , By default, the first digit of this number is always 1, So it can be Give up , Save only the back xxxxxx part . For example preservation 1.01 When the Hou , Save only 01, Wait until you read , Put the first 1 Add . The purpose of this , It's saving 1 Significant digits . With 32 position Floating point numbers, for example , Leave to M Only 23 position , Will come first 1 After giving up , It's equivalent to being able to save 24 Significant digits .
As for the index E, The situation is more complicated .
First ,E For an unsigned integer (unsigned int)
It means , If E by 8 position , Its value range is 0~255; If E by 11 position , Its value range is 0~2047. however , We know , In scientific counting E You can have negative numbers , therefore IEEE 754 Regulations , In memory E The true value of must be added with an intermediate number , about 8 Bit E, This middle number yes 127; about 11 Bit E, In the middle The number is 1023. such as ,2^10 Of E yes 10, So save it as 32 When floating-point numbers are in place , Must be saved as 10+127=137, namely 10001001.
Index E Fetching from memory can be further divided into three cases :
E Not all for 0 Or not all of them 1
At this time , Floating point numbers are represented by the following rules , The index E Of Subtract... From the calculated value 127( or 1023), obtain True value , then Significant figures M Add the first 1. such as : 0.5(1/2) The binary form of is 0.1, Since it is stipulated that the positive part must be 1, That is to move the decimal point to the right 1 position , Then for 1.0*2^(-1), Its order code is -1+127=126, Expressed as 01111110, And the mantissa 1.0 Remove the integer part and make it 0, A filling 0 To 23 position 00000000000000000000000, Then its binary representation is :0 01111110 00000000000000000000000
E All for 0
At this time , The exponent of a floating point number E be equal to 1-127( perhaps 1-1023) That's the true value , Significant figures M No more first 1, It's reduced to 0.xxxxxx Decimals of . This is to show that ±0, And close to 0 A very small number of .
E All for 1
At this time , If the significant number M All for 0, Express ± infinity ( It depends on the sign bit s);
Okay , How many floating-point rules are introduced .
Conclusion
Just to summarize , The storage of shaping is through In the form of complement ( Through the form of big and small ends ), Floating point storage mainly stores A sign bit E Binary bit level of M The exact number of digits . If you get something , Let's support San Lian !
边栏推荐
- Jvm-09 byte code introduction
- 【日常训练】395. 至少有 K 个重复字符的最长子串
- [Yu Yue education] scientific computing and MATLAB language reference materials of Central South University
- Functional modules and application scenarios covered by the productization of user portraits
- 详解指针进阶2
- 视觉上位系统设计开发(halcon-winform)-1.流程节点设计
- Introduction to redis master-slave, sentinel and cluster mode
- 函数栈帧的创建和销毁
- 官网MapReduce实例代码详细批注
- leetcode_ Power of Four
猜你喜欢
视觉上位系统设计开发(halcon-winform)-5.相机
Dataframe returns the whole row according to the value
Halcon and WinForm study section 1
Digital image processing -- popular Canny edge detection
Seckill system 2 redis solves the problem of distributed session
基础SQL教程
Kubernetes帶你從頭到尾捋一遍
Tensorflow realizes verification code recognition (I)
Detailed pointer advanced 2
Jvm-06-execution engine
随机推荐
Redis single thread problem forced sorting layman literacy
秒杀系统3-商品列表和商品详情
PyTorch crop images differentiablly
[cloud native training camp] module VIII kubernetes life cycle management and service discovery
Search in the two-dimensional array of leetcode sword offer (10)
[combinatorics] permutation and combination (set permutation, step-by-step processing example)
详解指针进阶1
Calibre LVL
Halcon与Winform学习第一节
redis单线程问题强制梳理门外汉扫盲
SQL server installation location cannot be changed
leetcode_ Power of Four
Jvm-03-runtime data area PC, stack, local method stack
Visual upper system design and development (Halcon WinForm) -1 Process node design
Nppexec get process return code
Halcon and WinForm study section 1
Visual upper system design and development (Halcon WinForm) -5 camera
Qt常用语句备忘
Jvm-06-execution engine
[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller