当前位置:网站首页>Storage of data
Storage of data
2022-07-03 08:03:00 【Generally, I passed by banyuanjun】
Now we know that c Languages have various types of data , It can be roughly divided into the following two : Integer family and floating point family ;
Integer family :
char,( In fact, it is used in the compiler ascii Code stored , So it belongs to integer )
unsigned char,
int
unsigned int
long
long long
short,
unsigned short
Floating point family
float;
double;
unsigned float;
unsigned double;
These types have two meanings :
1: Determine the size of space opened up by variables ;
2: Determine the perspective of variables on memory ;
I believe you all understand the size of the space you decide to open up , such as int open up 4 Bytes of space ,long open up 8 Bytes of space , So what is the perspective of variables on memory ?
Next, I'll talk slowly .
The storage of integers
Integer storage has been said before , Integers are stored in space and divided into original codes , Inverse code , Complement code , And the source code, inverse code and complement of positive numbers are the same , From positive numbers to 32/64 Binary bits of bit , The inverse complement of the original code of a negative number needs to be calculated , The highest bit of positive and negative binary numbers is the sign bit ,0 It means a positive number ,1 A negative number .
The original negative complement calculation method
1. Original code : Directly convert to binary numbers
2. Inverse code : The original code is in addition to the sign bit , According to the not ,0 Turn into 1,1 Turn into 0
3. Complement code : Inverse code +1
It may be hard to understand just seeing , Then I'll take a few real columns to deepen your understanding , The following figures are in 32 Compile in a bit environment , So binary bits are 32 Bit .
10
Original code :00000000000000000000000000001010
Inverse code :00000000000000000000000000001010
Complement code :00000000000000000000000000001010
-10
Original code :10000000000000000000000000001010
Inverse code :11111111111111111111111111111110101
Complement code :11111111111111111111111111111110110
And if you use char Types of data are different ,char Type is one byte , If you put an integer into a char In variables of type , Something funny will happen , such as :
#include<stdio.h>
int main()
{
char a = 300;
printf("%d", a);
return 0;
}Generally speaking , It should print out 300, But it actually prints out 44, Why is that ?

Here we need to talk about char The types are different ,char The type is only one byte , But in the compiler , An integer is of integer type by default , Yes 4 Bytes , But how can a byte variable store four bytes of data ? Then the compiler will truncate the data .
Let's calculate first 300 The original code of :00000000000000000000000100101100
and a yes char Type of , Only one byte , That is to say 8 individual bit position , Only the lower eight bits of the original code can be stored :00101100;
This is data truncation , But in printf in , The output is %d Data of type , Then the compiler will be right a Shape and improve the stored data , If the integer lifting variable is a signed number , Just add the highest sign bit , Unsigned numbers will be added 0, So here a High level replenishment will replenish 24 individual 0, That is to say :00000000000000000000000000101100;
Calculated 44;
Next, let me show you a topic :
int main()
{
unsigned char a = 200;
unsigned char b = 100;
unsigned char c = 0;
c = a + b;
printf(“%d %d”, a+b,c);
return 0;
}The output here is 300 and 44;
Let's calculate :
200 The complement of stores 00000000000000000000000011001000
100 The complement of stores 00000000000000000000000001100100
a The lower eight bits in the storage are 11001000;
b The lower eight bits in the storage are 01100100;
c=a+b, therefore c The original code of is :00101100;
stay printf in , First output a+b Value , and a+b Because the form of output is %d Integer type of ,
Therefore, it will be reshaped and improved , Both became 32 Add bit after bit , Therefore, the value will not be changed ,
Direct output 300, and c The original code of has become 00101100, After the integer is improved, it becomes 00000000000000000000000000101100, Calculated 44;
Floating point storage
The storage of floating-point numbers is very different from that of integers , although float The type is also 4 Bytes , But its accuracy is actually Limited , According to the standard , There is a rule for storing floating point numbers
Any floating point number v It can be expressed as (-1)^s*M*2^e;
among (-1)^s A symbol ,s = 0 Represents an integer ,1 A negative number ,
M Represents a significant number , Greater than or equal to 1, Less than or equal to 2;
2^e Then it means exponential
stay float Type in the ,32 individual bit In a , Highest bit storage s Value , After the highest position 8 Bit storage e Value , Remaining storage M Value ,
But for M and e, The standard has special provisions , such as , because M It must be 1 To 2 Between , So store m when , Will round off before the decimal point 1, Only the number after the decimal point is stored , And for e, There are three different rules .
e Is an index , It belongs to unsigned integer , But sometimes floating point numbers may be smaller than 1, So the index may need to be less than 1 Of , therefore e At the time of storage , Will add a median ,float Type plus 127,double Type plus 1023, Then save it into memory space .
Suppose a variable of floating-point number type is 5, Then calculate the binary sequence stored in memory according to the above standard
5 Converting to binary is 101, Convert to decimal 1.01*2^2;e by 2, You need to add 127,m by 1.01, Give up 1 by .01,5 Is an integer, so s by 0, So the binary sequence should be
0 10000001 010000000000000
We can do it in visual Check whether the memory window in debugging is this value , In memory is 16 Base store , Every time 4 A bit is a byte , So convert the above binary into 16 Hexadecimal should be
40 A0 00 00
Because my computer is small end storage , So it should be the reverse , Pictured

Of course , There are stored floating point numbers , Naturally, there is also knowledge of how to extract floating-point numbers from memory ,
Basically only e There are some special cases ,
1> e Not all for 0 Or not all of them 1
e subtract (127) perhaps (1023),M Add the number before the decimal places 1
2>e All for 0
e = 1-127 perhaps 1-1023,M Before the decimal places are not added 1, It means wireless approach 0 Number of numbers
3>e All for 1
If the significant figures are all 0, It means positive and negative infinity
The above is some small knowledge of data storage , Thank you for seeing here
边栏推荐
- Zohocrm deluge function application time verification
- 璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
- Quelle est la définition? Qu'est - ce qu'une déclaration? Quelle est la différence?
- How to clear the console password for s7700 device
- A tunnel to all ports of the server
- JS regular case-
- Transfinite hacker cognition
- Technical dry goods | some thoughts on the future of AI architecture
- Static keyword
- Pat grade a 1029 median
猜你喜欢

一条通往服务器所有端口的隧道
![[MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)](/img/38/3435d353e50b19fe09c8ab9db52204.png)
[MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)

Technology dry goods | Roberta of the migration of mindspore NLP model - emotion analysis task

Unity2019_ Lighting system

unity2019_ Input management

Unity2019_ Natural ambient light_ Sky box
![[MySQL 12] MySQL 8.0.18 reinitialization](/img/e1/9874df18bbc8d80c3c5c5fe39aefc9.png)
[MySQL 12] MySQL 8.0.18 reinitialization

IP production stream is so close to me

在浏览器输入url后执行什么

多旅行商问题——公式和求解过程概述
随机推荐
Redis batch startup and shutdown script
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
PostGIS space function
My touch screen production "brief history" 1
jsutlis
PIP uses image website to solve the problem of slow network speed
What is a data type? What is the use of data types?
[cocos creator] get the resource UUID
[untitled]
PHP常用排序算法
Harmonyos third training notes
LwIP learning socket (application)
Huawei switch: configure Telnet, SSH and web access
Register keyword
the installer has encountered an unexpected error installing this package
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
tslib库的移植
Viz artist advanced script video tutorial -- stringmap use and vertex operation
Wechat native applet cloud development learning record 01
Huawei switch console password reset, device initialization, default password