当前位置:网站首页>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 .
边栏推荐
- Jiuyi cloud black free encryption free version source code
- Tonybot humanoid robot checks the port and corresponds to port 0701
- Find specified characters
- Zzuli:1049 square sum and cubic sum
- 7-6 mixed type data format input
- 适用于XP的DDK
- 别再问自己适不适合做软件测试了
- Zzuli:1044 failure rate
- Zzuli:1057 prime number determination
- Address book sorting
猜你喜欢

tonybot 人形机器人 定距移动 代码编写玩法
![洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解](/img/89/da1a3a38e02671628f385de0f30369.png)
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解

分布式事务(Seata) 四大模式详解

Pyqt interface production (login + jump page)

Tonybot humanoid robot checks the port and corresponds to port 0701

提高效率 Or 增加成本,开发人员应如何理解结对编程?

Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules

dllexport和dllimport

Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion

How to query the baby category of tmall on Taobao
随机推荐
7-23 currency conversion (using array conversion)
FPGA blocking assignment and non blocking assignment
全文检索引擎Solr系列—–全文检索基本原理
Sendmail can't send mail and it's too slow to send. Solve it
Etcd cluster permission management and account password usage
Programming language: the essence of type system
MySQL multi table query subquery
论文分享:Generating Playful Palettes from Images
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
Doris学习笔记之数据表的创建
Why is this error reported when modifying records in the database
Stop asking yourself if you are suitable for software testing
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
Zzuli:1049 square sum and cubic sum
How Facebook moves instagram from AWS to its own server
556. 下一个更大元素 III
String sort
Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)
DDK for XP
Zzuli:1040 sum of sequence 1