当前位置:网站首页>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 :
边栏推荐
- USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate
- Lambda - 1
- MYSQL:Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column
- VirtualLab基础实验教程-4.单缝衍射
- Small program +app, a low-cost and active technology combination idea
- Random talk about redis source code 91
- A method of quickly reusing wechat login authorization in app
- es7不使用父子和嵌套关系来实现一对多功能
- Stack in JS (including leetcode examples) < continuous update ~>
- Remote gadget putty (Alibaba cloud mirror station address sharing)
猜你喜欢

VirtualLab basic experiment tutorial -4 Single slit diffraction

Eve-ng installation (network device simulator)

vant3+ts h5页面嵌套进app 与原生app通信

小程序+App,低成本获客及活跃的一种技术组合思路

有源差分晶振原理图以及LV-PECL、LVDS、HCSL区别

High-Speed Layout Guidelines 未完...

DRM driven MMAP detailed explanation: (I) preliminary knowledge

利用小程序快速生成App,只需七步

LCD parameter interpretation and calculation

ESP-IDF 添加自己的组件
随机推荐
TypeScript高级类型(二)
The server time zone value ‘�й���ʱ��‘ is unrecognized or represents more than one time zone. ......
Eve-ng installation (network device simulator)
USB to serial port - serial port driver type
Section qemu+gdb
Summary of static memory allocation and dynamic memory allocation
USB转串口那些事儿—串口驱动类型
C business serial number rule generation component
Gossip about the 88 of redis source code
Make good use of IDE, speed up R & D efficiency by 100%
NixOS 22.05安装过程记录
关于数据集
MySQL learning notes
同花顺能开户吗,同花顺在APP上可以直接开通券商安全吗
Applet and app are owned at the same time? A technical scheme with both
低代码平台调研结果
Leetcode 674 longest incrementing substring
Queue priority of message queue practice
JS中的栈(含leetcode例题)<持续更新~>
Overall flow chart of kernel interrupt


