当前位置:网站首页>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 !
边栏推荐
- GCC cannot find the library file after specifying the link library path
- Relationship between truncated random distribution and original distribution
- Puppet自动化运维排错案例
- Jvm-02-class loading subsystem
- Creation and destruction of function stack frames
- 整形和浮点型是如何在内存中的存储
- 使用JMeter对WebService进行压力测试
- What is label encoding? How to distinguish and use one hot encoding and label encoding?
- Redis cache penetration, cache breakdown, cache avalanche solution
- [transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
猜你喜欢
Basic SQL tutorial
Leasing cases of the implementation of the new regulations on the rental of jointly owned houses in Beijing
Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
秒杀系统3-商品列表和商品详情
Summary of concurrent full knowledge points
[pytorch learning notes] datasets and dataloaders
Didi off the shelf! Data security is national security
Popular understanding of decision tree ID3
VS2017通过IP调试驱动(双机调试)
【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer
随机推荐
CString中使用百分号
Popular understanding of linear regression (II)
Jvm-02-class loading subsystem
Popular understanding of linear regression (I)
Visual upper system design and development (Halcon WinForm) -3 Image control
Unity hierarchical bounding box AABB tree
详解指针进阶1
Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
Puppet automatic operation and maintenance troubleshooting cases
Baidu AI Cloud helps Shizuishan upgrade the smart health care model of "Internet + elderly care services"
[pytorch learning notes] datasets and dataloaders
win32创建窗口及按钮(轻量级)
App全局异常捕获
软件安装信息、系统服务在注册表中的位置
Construction of operation and maintenance system
[combinatorics] permutation and combination (set permutation, step-by-step processing example)
秒殺系統3-商品列錶和商品詳情
Concurrency-01-create thread, sleep, yield, wait, join, interrupt, thread state, synchronized, park, reentrantlock
解决pushgateway数据多次推送会覆盖的问题
在MapReduce中利用MultipleOutputs输出多个文件