当前位置:网站首页>C language learning -- data storage in memory

C language learning -- data storage in memory

2022-06-12 18:13:00 Learn about cust

Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right


Preface

Mainly learn how to store integer and floating-point data in memory and the corresponding code analysis .

One 、C Language data type

C There are four main types of languages :
1. Basic types :

(1) integer :

① Character integer char
② Short short
③ integer int
④ Long integer long
⑤ Longer integer long long
Be careful : Every type of integer data has signed、unsigned Points
When the symbol is not marked , The default is signed

(2) floating-point :

① Single precision floating point float
② Double precision floating point double

2. Construction type :

(1) An array type
(2) Type of structure : keyword struct
(3) Enumeration type : keyword enum
(4) Type of community : keyword union

3. Pointer types
4. Empty type :void

Why use data types ?

1. The data type determines the way and size of data stored in memory ;
2. The data type determines the perspective of memory space ;

Two 、 Storage of data in memory

This paper mainly discusses the storage of integer and floating-point data in memory .

1. Integer data

(1) Premise

When learning how to store integer data in memory , We need to clarify the following points first :
First of all , Integer data can be expressed in three forms : Original code 、 Inverse code 、 Complement code ;
second , The three representations are composed of sign bits and numeric bits , Sign bit 0 Stands for positive number , Sign bit 1 It's a negative number ;
Third , A positive number 、 Inverse code 、 The complement is the same ;
Fourth , Binary complement of integer data stored in memory ;

(2) Original code

The original code is to put a int Expand the data to 32 The binary number of bits .
for example :
-5 ——>10000000 00000000 00000000 00000101
5 ——> 00000000 00000000 00000000 00000101

(3) Inverse code

The inverse code is obtained by inversing every bit of the original code except the sign bit .
for example :
-5——>11111111 11111111 11111111 11111010
5 ——>00000000 00000000 00000000 00000101

(4) Complement code

Complement is the inverse code plus one
for example :
-5——>11111111 11111111 11111111 11111011
5 ——>00000000 00000000 00000000 00000101

(5) a key

①char The numerical range of the type

char Type takes only one byte (8 individual bit)
unsigned char:0-255

That is to say 11111111

signed char:-128-127

Its value variation range is :
 Insert picture description here

② Integer lifting and truncation

What is integer lifting ?

Integer promotion is when performing integer operations , Reduce the number of bytes to int Byte data type (short perhaps char) Convert to integer (int) type .

Why should we carry out integer upgrading ?

Because in the computer, there are CPU Operators in ALU Accomplished , and ALU It is specified in the design , The size of its operation object is at least int type ; meanwhile ,ALU The operation object of is stored in a register in memory , The byte size of the general register of the computer is the same as that of the arithmetic logic unit (ALU) Be consistent . So this requires for short perhaps char When performing integer operations on data of type , We must first promote them to integer int.

How to perform integer prompt ?

For signed data types , We need to fill the sign bit before its binary complement , Until will 8 individual bit Bit char Data complement 32 individual bit position , perhaps 16 individual bit For the purpose of short Type supplement 32 individual bit position ;

For unsigned data types , We need to fill in before its binary number 0, Until it is completed 32 individual bit position ;

truncation :

Truncation is storing integer data into short perhaps char Type variable , Keep only 16、8 individual bit position , Remove other bits . Note that the number of bits reserved starts from the low order of the binary value .

(6) Example

	char a = 255;
	unsigned char b = -1;

	printf("a = %u\n", a);
	printf("b = %u\n", b);

The analysis process :
first line :255 Binary source code of ( Complement code )1111 1111, Variable a It's symbolic char type , take 255 The complement of is stored in a in . because a A signed , So the first 1 It's a sign bit , here a The value represented shall be converted from complement to original code , namely 1000 0001, therefore a The median value is -1;
The second line :-1 The binary complement of is 1111 1111, Variable b It's unsigned char type . So we need a Medium value , Eight bit Bits are numeric bits , And there is no difference between the original code and the complement code , so b The median value is 255;
The third line : Printing involves shaping operations , First the a Make an integer upgrade ,a In the store 1111 1111, And a It's symbolic , Therefore, the sign bit is filled in the high position 1, obtain 11111111 11111111 11111111 11111111, And print in unsigned form , Therefore, the binary code has no distinction between the original code and the complement, and is all numeric bits , Print to get a huge number :4294967295;
In the fourth row : take b Make an integer upgrade ,b In the store 1111 1111, And b Unsigned , Fill directly in the high position 0, obtain 00000000 00000000 00000000 11111111, And print in unsigned form , So the binary bits are all numeric bits , And the complement of the original code does not distinguish , Print it out :255
result :
 Insert picture description here

	char a = 255;
	unsigned char b = -1;

	printf("a = %d\n", a);
	printf("b = %d\n", b);

analysis :
first line : ditto ;
The second line : ditto ;
The third line :a Make an integer upgrade , And a It's symbolic , Therefore, the sign bit is filled in the high position , obtain 11111111 11111111 11111111 11111111, Print in signed form , Because the first is 1, So it's negative , To convert the binary complement to the original code , obtain 10000000 00000000 00000000 00000001, So you can print :-1;
In the fourth row :b Make an integer upgrade , And b Unsigned , Therefore, it is necessary to fill in the high position 0, obtain 00000000 00000000 00000000 11111111. Print in signed form , Because the first is 0, So it's a positive number , The complement of the original code is consistent , Print it out :255;
result :
 Insert picture description here

原网站

版权声明
本文为[Learn about cust]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121804383054.html