当前位置:网站首页>Talking about part of data storage in C language
Talking about part of data storage in C language
2022-07-03 14:36:00 【roseisbule】
1. Shaping storage in memory
about c Shaping in language , We all know that it is possession 4 Bytes ,32 A bit , But how is a plastic surgery here 32 Stored in bits ?
First of all, we need to know the original code , Inverse code , The concept of complement . In the computer's memory , Most of the data is stored in complement . The calculation relationship of the three is : The original code is the binary of the original value , The inverse code is the original code, and the sign bit of the corresponding data type remains unchanged , Reverse other bits , The complement is the inverse plus 1. The original and inverse complements of positive numbers are the same , The above calculation is required for negative numbers . Why do these three concepts exist , This is because of the computer cpu Only the addition calculator , There is no subtraction calculator , Scientists to achieve subtraction , Set up these three concepts , because 8-3 Can be seen as 8+(-3).
Understand the original anti complement , Let's take an example .
I am here vs2022 Enter the following code in
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int c = -10;
return 0;
}
Start debug mode , Find... In the memory window a,b,c Of memory .( The memory here shows that I set it to 16 Base display )
This is a a Of memory
This is a b Of memory
This is a c Of memory
According to the previous introduction , The original code of a positive number , Inverse code , The complement is the same .
that 10 The binary of is 00000000000000000000000000001010
that 16 The base number is 0000000a
that 20 The binary of is 00000000000000000000000000010100
that 16 The base number is 00000014
that -10 The binary of is 10000000000000000000000000001010
The original inverse complement of negative numbers needs to be calculated
The inverse code is the sign bit of the corresponding type unchanged , Reverse other bits
Then the inverse code is 11111111111111111111111111110101
The complement is the inverse plus 1
Then the complement is 11111111111111111111111111110110
16 The base number is fffffff6
It is not difficult for us to find that it seems to be stored like this , But there is something wrong with the order .
This is about to introduce the concept of big end and small end storage .
Big end ( Storage ) Pattern , The low bit of data is stored in the high address of memory , And the high end of the data , Stored in memory lowlands Address .
The small end ( Storage ) Pattern , The low bit of data is stored in the low address of memory , And the high end of the data , Stored in memory Address .
For the complement we calculated 16 Base number , From left to right , The position of the data becomes lower , From left to right , The location of the address becomes higher , And my machine is a small end storage mode , This will cause the phenomenon of reverse release .
So far, we have learned how plastic surgery is stored .
2. The storage of character type in memory
I'm just talking about the number range that character types can store .
We know that the size of a character type is one byte ,8 A bit . So for unsigned character types , The minimum is 0, The biggest is 11111111( Binary system ) That is to say 255. Then the number that the unsigned character type can store is 0~255.
What about signed character types ?
Let's start with a piece of code
#include <stdio.h>
#include <string.h>
int main()
{
char a[1000];
int i;
for (i = 0;i < 1000;i++)
a[i]=-1-i;
printf("%d",strlen(a));
return 0;
}
255 yes vs2022 Results of operation .
But why .
Let's analyze ,i from 0 Start , Then keep on a Element assignment in array .
At first it was -1, And then -2, And then there was -3.......
Let's see where the maximum negative character type can go . Because the character type is a byte size ,8 A bit , The highest bit is the symbol bit .
Obviously 11111111 Minimum , namely -127, If this time again -1, became 100000000, Beyond the 8 A bit , What is this number ,c The rules of language stipulate that this number is -128, Cut him off into 10000000. And then we'll -1, became 01111111, That is to say 127. Then it will be reduced to 0, That's us strlen The sign of the end . Then we will store in this array strlen Calculable -1~-128 + 127~1, That is to say 255 Data .
3. Floating point memory storage
Floating point memory storage and shaping are very different from character type . For a single precision floating-point type , Size is 4 Bytes ,8 A bit . How to store this 8 A bit ,c The rules of language are as follows .
among S Symbol bit (s by 0 It's a positive number ,s by 1 It's negative ),E Is the number of digits ,M Is a significant digit .
How to understand ?
With -5.0 For example :-5.0 The binary of is -101.0, amount to -1.01*2^2(10 Into the system is 10,2 Into the system is 2), that S by 1,E by 2,M by 1.01
The difference between double precision and single precision is , The numbers are different
IEEE 754 There are some special provisions for floating-point numbers :
1. We can see in front M It is greater than 1, Less than 2 Of , So they are storing M When ,1.01 One of the first 1 It doesn't exist , The default is 1, Take it out when reading , The advantage of this is that it can only be stored 23 A significant , Now you can store 24 A significant .
2. To and E, In limine c The language stipulates that it is an unsigned number , But in scientific counting , We know ,E In truth , You can have negative numbers , If it is specified as a bit unsigned number , Obviously, it reduces the size range of floating-point data . therefore IEEE 754 May dictate ,E Add a middle number by default when storing , For single precision , This number is 127, For double precision , This number is 1023, This number is subtracted when reading , This ensures accuracy , It also ensures that there is no need to store negative numbers when storing .
3. about E Take out , There are three situations :
a.E Not all for 0 and E Not all for 1
Then subtract 127 or 1023 You can get the real value .
b.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 .
c.E All for 1
At this time ,M All for 0, Express ± infinity .
That's what I'm talking about c Simple cognition of some data types in language .
边栏推荐
- Luogu p4047 [jsoi2010] tribal division solution
- 数学常数表 by q779
- Common commands for getting started with mongodb database
- 556. The next larger element III
- Tonybot humanoid robot starts for the first time 0630
- 添加Zabbix计算类型项目Calculated items
- 如何查询淘宝天猫的宝贝类目
- Find the sum of the elements of each row of the matrix
- Analysis of gene family characteristics - chromosome location analysis
- 7-3 count the number of words in a line of text
猜你喜欢
Why is this error reported when modifying records in the database
tonybot 人形机器人 红外遥控玩法 0630
Sword finger offer 28 Symmetric binary tree
Analysis of gene family characteristics - chromosome location analysis
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
adc128s022 ADC verilog设计实现
一文了解微分段应用场景与实现机制
7-15 calculation of PI
Doris学习笔记之数据表的创建
dllexport和dllimport
随机推荐
Jiuyi cloud black free encryption free version source code
Dllexport and dllimport
动态获取权限
7-20 print 99 formula table (format output)
Pyqt interface production (login + jump page)
Zzuli:1042 sum of sequence 3
天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
Programming language: the essence of type system
556. 下一个更大元素 III : 简单构造模拟题
Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
7-24 reduction of the simplest fraction (rolling Division)
Find the sum of the elements of each row of the matrix
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
Although not necessarily the best, it must be the hardest!
Etcd cluster permission management and account password usage
Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
Zabbix添加Calculated items后保存页面成空白
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
Add ZABBIX calculation type itemcalculated items
tonybot 人形机器人 定距移动 代码编写玩法