当前位置:网站首页>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 .
边栏推荐
- Win10 drqa installation
- BlockingQueue source code
- Uniapp (upload local pictures, preview pictures, convert Base64 format, upload audio files)
- Detailed explanation of the player network data reading process of ijkplayer code walkthrough 2
- Recyclerview has data flicker problem using databinding
- 【sketchup 2021】草图大师中CAD文件的导入与建模(利用cad图纸在草图大师中建立立面模型)、草图大师导出成品为dwg格式的二维、三维、立面效果到cad中打开预览】
- Kotlin collaboration - simple use of collaboration
- 【sketchup 2021】草图大师的图像输出与渲染之样式说明【边线设置、平面设置、背景设置、水印设置、建模设置、天空背景创建天空、利用水印背景创建天空(重要)】
- Subtotal of constraintlayout
- Detailed explanation of scrcpy client code walk through H264 raw stream decoding process
猜你喜欢
MFS explanation (V) -- MFS metadata log server installation and configuration
电镀挂具RFID工序管理解决方案
Solution: vscode open file will always overwrite the last opened label
端午安康,使用祝福话语生成词云吧
In kotlin?,!,?:,:, - & gt;、== Brief description of symbols
Learning records countless questions (JS)
Wechat applet: click the event to obtain the current device information (basic)
SSM框架整合--->简单后台管理
【虚拟机】 VMware虚拟机占用空间过大解决
JVM Foundation
随机推荐
Use of kotlin basic common sets list, set and map
Detailed explanation of the player network data reading process of ijkplayer code walkthrough 2
Super model logo online design and production tool
High burst solution 2
[SketchUp 2021] CAD file import and modeling in the sketch master (establish elevation model in the sketch master by using CAD drawings), and the sketch master exports 2D, 3D and elevation effects of
Use of smalidea
MFS詳解(七)——MFS客戶端與web監控安裝配置
Interface oriented programming in C language
Base64 principle
JNI exception handling
[MySQL] basic knowledge review
ADB shell CMD overlay debugging command facilitates viewing system framework character resource values
Wechat applet (get location)
《MATLAB 神经网络43个案例分析》:第11章 连续Hopfield神经网络的优化——旅行商问题优化计算
SSM框架整合--->简单后台管理
JNI's bitmap grayscale image (rgba_8888 and rgb_565)
Dart class inherits and implements mixed operators
JVM基础
Hbuilderx: installation of hbuilderx and its common plug-ins
Applet Use of spaces