当前位置:网站首页>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
List of articles
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 double2. Construction type :
(1) An array type
(2) Type of structure : keyword struct
(3) Enumeration type : keyword enum
(4) Type of community : keyword union3. 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-255That is to say 11111111
signed char:-128-127
Its value variation range is :
② 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 :
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 :
边栏推荐
- 企业架构的第一性原理
- 网盘和对象云存储管理之磁盘映射工具比较
- High-Speed Layout Guidelines 未完...
- Introduction to cookie usage
- 极限编程--根源分析实践
- Bug record: data truncation: incorrect datetime value:
- MYSQL:Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column
- Idea common shortcut keys
- A story on the cloud of the Centennial Olympic Games belonging to Alibaba cloud video cloud
- 迄今微软不同时期发布的SQL Server各版本之间的大致区别,供参考查阅
猜你喜欢
es7不使用父子和嵌套关系来实现一对多功能
USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate
用好IDE,研发效能提速100%
Variable of C #
The server time zone value ‘�й���ʱ��‘ is unrecognized or represents more than one time zone. ......
Schedule update | 2022 Microsoft and Intel hacker song competition is in hot registration
Click the list page of vant3+ts+pinia tab to enter the details. The tab on the details page is highlighted in the original position, and the refresh highlight is in the first item by default
VirtualLab基础实验教程-4.单缝衍射
MYSQL:Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column
Typescript type declaration file (III)
随机推荐
干货 | 一文搞定 pytest 自动化测试框架(二)
机器学习系列(3):Logistic回归
Codeforces Round #398 (Div. 2) D. Cartons of milk
Write a select based concurrent server
USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate
HTTP cache < strong cache and negotiation cache >
VirtualLab基础实验教程-5.泊松亮斑
Click the list page of vant3+ts+pinia tab to enter the details. The tab on the details page is highlighted in the original position, and the refresh highlight is in the first item by default
es7不使用父子和嵌套关系来实现一对多功能
Extreme Programming -- Practice of root cause analysis
leetcode 674 最长递增子串
01-复杂度
Introduction to reinforcement learning and analysis of classic items 1.3
Schedule update | 2022 Microsoft and Intel hacker song competition is in hot registration
vant3 +ts 封装简易step进步器组件
leetcode491 递增子序列
在同花顺开户证券安全吗
Make good use of IDE, speed up R & D efficiency by 100%
App中快速复用微信登录授权的一种方法
leetcode151 翻转字符串里的单词