当前位置:网站首页>Advanced C language - Section 1 - data storage
Advanced C language - Section 1 - data storage
2022-06-13 04:47:00 【Stretch the curtain with the wind】
Catalog
1.1. Basic classification of types
2.1. Original code 、 Inverse code 、 Complement code
2.1.1. Original code 、 Inverse code 、 Complement introduction
2.1.2. Why is a complement stored in memory
2.2. Introduction to big and small end
2.2.2. Why there are big end and small end
3.3.3. Design a small program to determine the current machine byte order
2.3. practice (※)
3. Floating point storage in memory
3.1. An example (※)
3.2. Floating point storage rules
1. Data type introduction
char // Character data typeshort // Shortint // plasticlong // Long integerlong long // Longer plastic surgeryfloat // Single-precision floating-pointdouble // Double precision floating point
c99 Boolean types are introduced in : _Bool, Its function is to store true and false values , As shown in the figure below ( In the figure true Namely 1,false Namely 0)( In fact, there is basically no boolean type , because int Type can completely solve , And Boolean types are essentially right int Type has been renamed )
Need to quote stdbool.h The header file


1.1. Basic classification of types
1. Plastic surgery Family :
charunsigned char char What is the signed char still unsigned char It depends on the compiler implementationsigned char vs In the compiler char And signed char Equivalentshortunsigned short [int] Unsigned shortsigned short [int] Signed short integers short And signed short Equivalentintunsigned int Unsigned integersigned int signed int int And signed int Equivalentlongunsigned long [int] Unsigned longsigned long [int] Signed long integer long And signed long Equivalentlonglong
floatdouble
3. Construction type / Custom type
> An array type : Remove the array name , The rest is the type of array ( int a[10] The type is int [10] )> Type of structure struct> Enumeration type enum> Joint type union
4. Pointer types
int* pi;char* pc;float* pf;void* pv;
void Indicates empty type ( No type )Usually applied to the return type of a function 、 The parameters of the function 、 Pointer types

2. Shaping storage in memory
2.1. Original code 、 Inverse code 、 Complement code
2.1.1. Original code 、 Inverse code 、 Complement introduction
Integers There are three forms of binary representation of : Original code 、 Inverse code 、 Complement codeWhat is stored in memory is a binary complement
Integers are divided into positive integers and negative integersPositive integer : Original code 、 Inverse code 、 The complement is the same15 The original code of : 00000000 00000000 00000000 0000111115 The inverse of : 00000000 00000000 00000000 0000111115 Complement : 00000000 00000000 00000000 00001111Negtive integer : Original code 、 Inverse code 、 The complement needs to be calculated , as followsOriginal code : The binary code written directly according to the positive and negative of a number is the original code-15 The original code of :10000000 00000000 00000000 00001111Inverse code : The sign bits remain the same , The reverse of other bits is the reverse code-15 The inverse of :11111111 11111111 11111111 11110000Complement code : The binary sequence of the inverse code plus one is the complement-15 Complement :11111111 11111111 11111111 11110001
2.1.2. Why is a complement stored in memory
In computer system , All values are represented and stored by complements . The reason lies in , Use complement , Symbol bits and value fields can be treated in a unified way ; meanwhile , Addition and subtraction can also be handled in a unified way (CPU Only adders ) Besides , Complement code and original code are converted to each other , Its operation process is the same , No need for additional hardware circuits .
The man goes next door 1-1, Because there are only adders in the computer , So it is 1+(-1), Let's try to calculate with the original code and the complement code
2.2. Introduction to big and small end



summary : We find that the numbers in the memory are always stored backwards
2.2.1. Big end and small end
int a = 0x11223344;Big end byte order storage : When the low byte data of a data is stored at the high address , The contents of the high byte order are placed at the low address , This storage method is large end byte order storage Memory : 11 22 33 44
Small end byte order storage : When a low byte of data is stored at a low address , The contents of high byte order are placed at the high address , This storage method is small end byte order storage Memory :44 33 22 11
2.2.2. Why there are big end and small end
Why are there big and small end patterns ? This is because in a computer system , We are in bytes , Each address corresponds to a byte , A byte is 8bit. But in C In language, except 8 bit Of char outside , also 16 bit Of short type ,32 bit Of long type ( It depends on the compiler ), in addition , For digits greater than 8 Bit processor , for example 16 Bits or 32 Bit processor , Because the register width is larger than one byte , So there must be a problem of how to arrange multiple bytes . So it leads to big end storage mode and small end storage mode .for example : One 16bit Of short type x , The address in memory is 0x0010 , x The value of is 0x1122 , that 0x11 For high byte , 0x22 Is low byte . For big end mode , will 0x11 Put it in the low address , namely 0x0010 in ,0x22 Put it in a high address , namely 0x0011 in . The small end model , Just the opposite . That we use a lot X86 The structure is small end mode , and KEIL C51 It's the big end mode . A great deal of ARM,DSP It's all small end mode . There are some ARM The processor can also choose the big end mode or the small end mode by the hardware .
2.2.3. Design a small program to determine the current machine byte order
Code 1:



2.3. practice
Calculate the output of the following code
#include <stdio.h>
int main()
{
char a= -1;
signed char b=-1;
unsigned char c=-1;
printf("a=%d,b=%d,c=%d",a,b,c);
return 0;
}answer :
![]()
notes :
1. char a=-1 and signed char b=-1 and unsigned char c=-1
-1 The original code of :10000000000000000000000000000001
Calculate the output of the following code
#include <stdio.h>
int main()
{
char a = -128;
printf("%u\n",a);
return 0;
}answer
![]()
notes :
1.char a = -128
-128 The original code of :10000000000000000000000010000000
Calculate the output of the following code
#include <stdio.h>
int main()
{
char a = 128;
printf("%u\n",a);
return 0;
}answer :
![]()
notes :
1.char a = 128
128 The original code of :00000000000000000000000010000000
128 Complement :00000000000000000000000010000000
Calculate the output of the following code
#include <stdio.h>
int main()
{
int i = -20;
unsigned int j = 10;
printf("%d\n", i + j);
return 0;
}answer :
![]()
notes :
1.int i = -20
-20 The original code of :10000000000000000000000000010100
Calculate the output of the following code
#include <stdio.h>
int main()
{
unsigned int i;
for (i = 9; i >= 0; i--) {
printf("%u\n", i);
}
return 0;
}answer : Dead cycle

notes :
1. When i by 0 When ,i-- by -1
-1 Complement :11111111111111111111111111111111
Calculate the output of the following code
#include <stdio.h>
int main()
{
char a[1000];
int i;
for (i = 0; i < 1000; i++)
{
a[i] = -1 - i;
}
printf("%d", strlen(a));
return 0;
}answer :
notes :
1. One char What values can be put in variables of type
char The type is one byte and eight bits , Put data ( All complement ) There are the following possibilities


For signed char In terms of type , The complement is converted to the original code , The data range is as follows ( Specify therein 10000000 The system resolves to by default -128)

summary :

(2) And char Empathy , Deduce other types of numerical ranges
For signed char, Its value range is -128~127
For signed short, Its value range is -32768~32767 (-32768 by 1000000000000000 Directly parse to get )

2. therefore char a[1000] The data stored in it should be
| -1 | -2 | -3 | -4 | -5 | ... | -128 | 127 | 126 | ... | 3 | 2 | 1 | 0 | -1 | -2 | ... |
| 0 | 1 | 2 | 3 | 4 | ... | 127 | 128 | 129 | ... | 252 | 253 | 254 | 255 | 256 | 257 | ... |
3. It's time to find the string '\0' By , Statistics '\0' Number of previous values , Actually '\0' Of ASCII The code value is 0, So to 0 end , It can be seen from the above table that 0 In front of you are 255 Number .
7. Practice seven
Calculate the output of the following code
#include <stdio.h>
unsigned char i = 0;
int main()
{
for (i = 0; i <= 255; i++)
{
printf("hello world\n");
}
return 0;
}answer : Dead cycle

notes : Unsigned char, Its value range is 0~255, To 255 The next value is 0, The cycle cut-off condition can never be reached , So the loop
3. Floating point storage in memory
Common floating point numbers :3.141591E10( intend 1.0)
The family of floating-point numbers includes : float、double、long double typeThe range represented by floating point numbers :float.h In the definition of , As shown in the figure below

3.1. An example (※)
int main()
{
int n = 9;
float *pFloat = (float *)&n;
printf("n The value of is :%d\n",n);
printf("*pFloat The value of is :%f\n",*pFloat);
*pFloat = 9.0;
printf("num The value of is :%d\n",n);
printf("*pFloat The value of is :%f\n",*pFloat);
return 0;
}Running results :

notes :
1.%f and %lf Will default to have after the decimal point 6 position
2. We found that using floating-point pointers for storage n The address of , The dereferenced value is not 9 or 9.0, We can infer that floating-point and integer types are stored differently in memory .
3. In the above code *pFloat It is to dereference from the perspective of floating-point numbers
When n=9 when
n The original code of :00000000000000000000000000001001
n Complement :00000000000000000000000000001001
| 0 | 00000000 | 00000000000000000001001 |
| S | E | M |
E For zero , The resulting value is a very close one 0 The number of , So printing out is 0.000000
When n=9.0 when
Decimal system 9.0 Convert to binary to 1001.0 Convert to scientific count to :
*1.001*
,S=1,M=1.001,E=3
| 0 | 3+127=130 Binary system :10000010 | 001 Binary system :00100000000000000000000 |
| S | E | M |
So at this time n by :0100 0001 0001 0000 0000 0000 0000 0000
n The hexadecimal of is 4 1 1 0 0 0 0 0 namely 0x41100000

From the picture above , Floating point storage also conforms to small segment storage
Put... In memory n Value 01000001000100000000000000000000 use %d out , At this time, the original reverse complement is the same , The value obtained is 1091567616
3.2. Floating point storage rules
According to international standards IEEE( Institute of electrical and Electronic Engineering ) 754, Any binary floating point number V It can be expressed in the following form :(-1)^S * M * 2^E
(-1)^s The sign bit , When s=0,V Is a positive number ; When s=1,V It's a negative number .
M Represents a significant number , Greater than or equal to 1, Less than 2.
2^E Indicates the index bit .
| The number | 1 | 1 | 1 | . | 1 | 1 | 1 |
| The weight | 2^2 | 2^1 | 2^0 | . | 2^(-1)=0.5 | 2^(-2)=0.25 | 2^(-3)=0.125 |
for example : Decimal 5.5 Convert to binary to 101.1,101.1 It can be written.
*1.011*
(S=0,M=1.011,E=2)

The highest 1 Bits are sign bits s, And then 8 Bits are exponents E, The rest 23 Bits are significant numbers M.

The highest 1 Bits are sign bits S, And then 11 Bits are exponents E, The rest 52 Bits are significant numbers M.
As I said before , 1≤M<2 , in other words ,M It can be written. 1.xxxxxx In the form of , among xxxxxx Represents the fractional part .IEEE 754 Regulations , Keep it in the computer M when , By default, the first digit of this number is always 1, So it can be discarded , Save only the back xxxxxx part . For example preservation 1.01 When , Save only 01, Wait until you read , Put the first 1 Add . The purpose of this , It's saving 1 Significant digits . With 32 For example, a floating-point number , Leave to M Only 23 position , Will come first 1 After giving up , It's equivalent to being able to save 24 Significant digits .
First ,E For an unsigned integer (unsigned int)It means , If E by 8 position , Its value range is 0~255; If E by 11 position , Its value range is 0~2047. however , We know , In scientific counting E You can have negative numberssuch as :Decimal system 0.5 Convert to binary to 0.1,0.1 Convert the scientific count to :*
therefore IEEE 754 Regulations , In memory E The true value of must be added with an intermediate number , about 8 Bit E, The middle number is 127; about 11 Bit E, The middle number is 1023.
such as :Decimal system 0.5 Of E The real value of is -1, And deposit E The real value should be stored in + The middle number (float:-1+127=126 double:-1+1023=1022)
Floating point numbers are represented by the following rules , The index E The calculated value of minus 127( or 1023), Get the real value , And then the significant number M Add the first 1
The exponent of a floating point number E be equal to 1-127( perhaps 1-1023) That's the true value , Significant figures M No more first 1, It's reduced to 0.xxxxxx Decimals of . This is to show that ±0, And close to 0 A very small number of .
At this time , If the significant number M All for 0, Express ± infinity ( It depends on the sign bit s)
边栏推荐
- 推荐的图片临时在线压缩工具
- Blockly learning ----1 Work area, block, toolbox
- Use go to add massive data to MySQL
- 2022年氧化工艺操作证考试题库及模拟考试
- 一致性哈希的简单认识
- Ctfshow SQL injection (231-253)
- On switch() case statement in C language
- QT client development -- driver loading problem of connecting to MySQL database
- Tita绩效宝:远程一对一面谈的问题
- rust编程-链表:使用struct实现链表,使用堆合并k个升序链表,自定义Display
猜你喜欢

Li Kou brush question 338 Bit count

Colab使用教程(超级详细版)及Colab Pro/Pro+评测

How to use redis

Internet people a few years ago vs Internet people today

CMB written test graphical reasoning

PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes

PowerShell: because running scripts is prohibited on this system, the solution

利用Javeswingjdbc基於mvc設計系統

Advantages of win8.1 and win10

约瑟夫问题
随机推荐
Ctfshow SQL injection (211-230)
Blockly learning ----2 Code generation, grid, scaling, events, storage
Gets or sets the content in an object
【Flutter 問題系列第 67 篇】在 Flutter 中使用 Get 插件在 Dialog 彈窗中不能二次跳轉路由問題的解决方案
SQL notes
Returns the width and height of an element
Go/golang connection to database
On switch() case statement in C language
How to lay copper in AD (aluminum designer)
Collection of some compatibility issues
PowerShell:因为在此系统上禁止运行脚本,解决方法
Flutter dart variables and constants
QT client development -- driver loading problem of connecting to MySQL database
Analysis on the similarities and differences of MVC, MVP and mvvc
Normal distribution (Gaussian distribution)
shell变量学习笔记
你的一对一会议效率低下,你可以这么做!
Common skills in embedded programming
Record a troubleshooting process - video call cannot be picked up
PHP development 16 exit module

)
(-1)^S * M * 2^E 