当前位置:网站首页>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
边栏推荐
- Microsoft Security Response Center
- Redis batch startup and shutdown script
- Wechat applet taro learning record
- How to configure GDAL under idea
- L'installateur a été installé avec une erreur inattendue
- P2704 [noi2001] artillery position (shape pressure DP)
- tp3.2和tp5.0的区别
- the installer has encountered an unexpected error installing this package
- Wechat native applet cloud development learning record 01
- Huawei switches are configured with SSH login remote management switches
猜你喜欢

L'installateur a été installé avec une erreur inattendue
![[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18](/img/9b/db5fe1a37e0de5ba363f9e108310a5.png)
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18

IP production stream is so close to me

Ventuz Foundation Series "one step at the door"

What to do after the browser enters the URL

An intern's journey to cnosdb

WorldView卫星遥感影像数据/米级分辨率遥感影像

C语言-入门-精华版-带你走进编程(一)

vcs import src < ros2. Repos failed

一条通往服务器所有端口的隧道
随机推荐
Redis batch startup and shutdown script
使用 FileChannel 进行文件的复制拷贝
Idea dereference display effect
haproxy+keepalived搭建01
the installer has encountered an unexpected error installing this package
Mutual call between Lua and C #
Unity performance optimization
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
My touch screen production "brief history" 1
P2622 关灯问题II(状态压缩 搜索)
Usage of (case, when) in PostgreSQL
When did you find out that youth was over
Huawei switches are configured with SSH login remote management switches
【LeetCode】4. Best time to buy and sell stock
L'installateur a été installé avec une erreur inattendue
P2704 [NOI2001] 炮兵阵地(状压dp)
[at] ABC 258g - triple Reach - violence
Redis查看客户端连接
P2622 light off problem II (state compression search)
C语言-入门-精华版-带你走进编程(一)