当前位置:网站首页>Data storage in memory (C language)
Data storage in memory (C language)
2022-06-13 06:36:00 【I will write bugs today】

hello, I am a bug. Today, let's learn about How data is stored in memory :
List of articles
One 、 data type
One 、 Introduce
Data types can be roughly divided into built-in types and user-defined types , Built in types are divided into three major types : integer 、 Floating point numbers 、 Pointer types . ️️


Two 、 The meaning of type
1、 Determine the size of the space created with this type of data .
2、 How to look at the perspective of memory space .
Be careful ️ ️
Character types are also a member of the integer family , Because the essence of storing character types is to store their ASCII Code value . ️️
Two 、 The storage of integers in memory
Original code 、 Inverse code 、 Complement code
Encoding mode : Convert data into binary by certain means , And this method is called coding method .
1、 Original code
Because the computer can only recognize 0 and 1, So the stored data can only be stored in binary . Then we have to convert the data into binary form . For positive numbers , Its binary number can be directly converted . So how to realize the binary of negative numbers ? To represent a negative number , People added a sign bit before binary numbers . This encoding method is called the original code , The appearance of the original code can solve the problem of computer storage , But the question that follows is , How to calculate it ? The arithmetic logic unit in a computer cannot directly calculate subtraction , To calculate subtraction, we have to convert it into addition . ️️
When we add with the original code , Pictured ️ ️:
There is no problem with the result of the addition calculation , Then let's do the subtraction , Pictured ️ ️:

For subtraction , We must first convert it into an addition operation . When we think about sign bits , The calculation result is obviously wrong ! Therefore, the original code cannot be used for the data stored by the computer .
2、 Inverse code
Because the original code of subtraction is not applicable , People invented the inverse code to solve this problem .
The inverse code is calculated as : The inverse of a positive number is itself , The inverse of a negative number is that the sign bit remains unchanged , Other bits are reversed .
When subtracting an inverse code , Pay attention to the following points :
1、 The sign bit and the data participate in the calculation .
2、 If carry occurs after the sign bits are added , Then we should send it to the lowest to carry .
3、 The result of inverse code operation is still inverse code . To get the original code , The key is to look at the sign bit , Symbol bit 0, So the inverse code is the same as the original code ; Symbol bit 1, Then we should negate the inverse code by bit .
The addition operation of the inverse code is no different from the original code , Here we look directly at subtraction , Pictured ️ ️:
There is no problem with the calculation results , So the inverse code can solve the subtraction operation very well . But in certain situations , There are still some small problems , Pictured ️ ️:
After the calculation result is reversed , There is -0 This situation .0 Why is there a difference between positive and negative ? For us , Plus or minus 0 There may be no difference . But for computers . Complement is all 0 And all 1 When they are, they mean +0 and -0, For such meaningless things , It still needs to be solved . So the complement appears .
3、 Complement code
The complement is calculated by : The complement of a positive number is the same as the original code , Negative numbers are complemented by adding one to the inverse code .
When subtracting a complement , Pay attention to the following points :
1、 The sign bit and the data participate in the calculation .
2、 If carry occurs after the sign bits are added , Then we should abandon it directly .
3、 The result of the inverse operation is also a complement . To get the original code , The key is to look at the sign bit , Symbol bit 0, Then the complement code is the same as the original code ; Symbol bit 1, Then the complement of the complement is the original code .
There is no difference between the addition of complement and the original code , Here we look directly at subtraction , Pictured ️ ️:
There is no problem with the result of calculation , Look again at 0 Sign problem , Pictured ️ ️:
In the calculation result 0 The sign problem of is also solved .
The advantages of complement :
1、 Use complement , Can solve the inverse code representation 0 The positive and negative representations of .
2、 The number range represented by complement code is larger than that of original code and inverse code .
summary :
In memory , Storing integer data is a stored complement . Storing integer data using complements can handle both symbolic and numeric fields ; At the same time, addition and subtraction can also be handled in a unified way , because cpu Only adders , So avoid subtraction directly . And the conversion process between the original code and the complement code is also consistent , So no additional hardware circuits are required .
3、 ... and 、 Byte order at the large and small end
Byte order at the large and small end :( stay C++ Mentioned in , Here, let's briefly sort out )
In a computer, we use bytes , Each address unit corresponds to a byte . about char This one byte type , There is no need to consider the order of bytes in memory when storing data . But for short、int And other types that occupy multiple bytes , How to arrange the order of data bytes is an inevitable problem .
Big end storage : That is, the high order of data is stored in the low order of memory , The lower bits of data are stored in the upper bits of memory .
Small end storage : That is, the high order of data is stored in the high order of memory , The low order of data is stored in the low order of memory .
Pictured ️ ️:

Through the memory window, we can see , The address increases gradually from left to right , On low position 44 The low bit stored in the address , So it is currently a small end storage .
Be careful ️ ️
That we use a lot X86 The structure is small end mode , and KEIL C51 It's the big end mode . A great deal of ARM,DSP It's all small end mode . There are some ARM The processor can also choose the big end mode or the small end mode by the hardware .
So how do we design a program to judge whether the current machine is a big end or a small end ? ️ ️
char Check()
{
int a = 1;
return *(char*)&a;
}
int main()
{
if (Check())// For real, it is a small terminal
{
printf(" The small end \n");
}
else// Big terminal
printf(" Big end \n");
return 0;
}
When we int Form storage of 1 when , So we used 4 Bytes to store . We then force the address to *char, After strong rotation, we can dereference the data on the lowest bit , So if it's a big terminal , So what is stored in the lower order is 00, If it is a small terminal , So what is stored in the lower order is 01, In this way, it can be distinguished .
Four 、 Floating point storage in memory
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 ️ ️:
With 6.0 For example , It's written in binary as 110.0, namely 1.1X2^2 . that S = 0,M = 1.1,E = 2 .
With -6.0 For example , It's written in binary as -110.0, namely -1.1X2^2 . that S = 1,M = -1.1,E = 2 .
So how do we store floating point numbers in memory ?
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.
In addition to the above ,IEEE 754 For significant figures M And the index E, There are some special rules .
1、M: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 discarded , Save only the back xxxxxx part . For example preservation 1.01 When , Save only 01, Wait until you read , Put the first 1 Add . The purpose of this , It's saving 1 Significant digits . With 32 For example, a floating-point number , Leave to M Only 23 position , Will come first 1 After giving up , It's equivalent to being able to save 24 Significant digits .
2、E: 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. But 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, The middle number is 127; about 11 Bit E, The middle 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.
With 6.5f For example , After it is converted into binary, it is expressed as 1.101X2^2, that S = 0,M = 1.101,E = 2 + 127 = 129. After saving it , Pictured ️ ️:
Let's look at the memory in the compiler , Pictured ️ ️:


Because the current machine is a small terminal machine ,6.5f The hexadecimal representation of is 0x40d00000, To store in the memory, it is necessary to store according to the small end .
then , take E There are also the following rules for taking out :
(1)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 The calculated value of minus 127( or 1023), Get the real value , And then the significant number M Add the first 1.
(2)E For all 1
At this time , If the significant number M All for 0, Express ± infinity ( It depends on the sign bit s).
(3)E For all 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 .
When we understand the storage of integer and floating-point numbers in memory , Try the following question .
Please write the output value of the following program : ️ ️
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.0f;// Let's just add f, It is said to be float
// *pFloat = 9.0; Because the default floating-point number type in the current compiler is double, So implicit type conversion occurs here , the double Turn it into float
printf("num The value of is :%d\n",n);
printf("*pFloat The value of is :%f\n",*pFloat);
return 0; }
The output result is :9 、0.000000 、1,091,567,616、9.000000
analysis :
n Is a positive number , that n The original code of 、 Complement code 、 The reverse code is consistent .n The complement of is :1001 ( Ahead 0 Omit ). At this time , We will n Forced address conversion of float The pointer to , So we're going to n Put the value of into *pfloat in , here E For all 0, So when you take it out in the form of a floating-point number , The number is infinitely close to 0, So the result is 0.000000 .
When we give *pfloat Deposit in 9.0f when , Its binary is :0 10000010 00100000000000000000000, Hex is :0x41100000, Take out according to the integer , So that is 1,091,567,616 .
verification :️ ️
Today's content is over here , I hope you can gain something .
边栏推荐
- Wechat applet development (requesting background data and encapsulating request function)
- [MySQL] basic knowledge review
- Relationship between fragment lifecycle and activity
- 【案例】一个超级好用的工具 —— 给程序员用的计算器
- Echart histogram: stack histogram value formatted display
- ADB shell sendent debug input event
- 1+1 > 2, share creators can help you achieve
- 欧姆龙平替国产大货—JY-V640半导体晶元盒读写器
- Omron Ping replaces the large domestic product jy-v640 semiconductor wafer box reader
- Dynamic link library nesting example
猜你喜欢

Construction and verification of Alibaba cloud server webrtc system

【Kernel】驱动编译的两种方式:编译成模块、编译进内核(使用杂项设备驱动模板)

JS convert text to language for playback

Two uses of bottomsheetbehavior

c语言对文件相关的处理和应用

【新手上路常见问答】一步一步理解程序设计

‘ipconfig‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。

【2022高考季】作为一个过来人想说的话
![[2022 college entrance examination season] what I want to say as a passer-by](/img/d7/3813b944dc2df182455d475a3669fb.jpg)
[2022 college entrance examination season] what I want to say as a passer-by

Recommend a capacity expansion tool to completely solve the problem of insufficient disk space in Disk C and other disks
随机推荐
Recent problems
The processing and application of C language to documents
Applet pull-up loading data
[JS] array flattening
Kotlin collaboration - simple use of collaboration
【sketchup 2021】草图大师中CAD文件的导入与建模(利用cad图纸在草图大师中建立立面模型)、草图大师导出成品为dwg格式的二维、三维、立面效果到cad中打开预览】
1+1 > 2, share creators can help you achieve
时间格式化工具----moment.js(网页时间实时展示)
JetPack - - - Navigation
Time complexity and space complexity
BlockingQueue源码
Kotlin foundation extension
Relationship between fragment lifecycle and activity
Error in downloading opencv from pip
《MATLAB 神经网络43个案例分析》:第10章 离散Hopfield神经网络的分类——高校科研能力评价
Use of smalidea
AI realizes "Resurrection" of relatives | old photo repair | old photo coloring, recommended by free app
Applet Use of spaces
[JS] handwriting call(), apply(), bind()
ADB shell content command debug database