当前位置:网站首页>Data storage [C language]
Data storage [C language]
2022-07-18 15:37:00 【Travel fool】
Catalog
One 、 Data type introduction
Basic built-in type :
char // Character data type
short // Short
int // integer
long // Long integer
long long // Longer integers
float // Single precision floating point
double // Double precision floating point
The meaning of type :
1. Use this type to exploit the size of memory space ( Size determines the range of use ).
2. How to look at the perspective of memory space .
1. Basic classification of types :
Plastic surgery Family :
char // The essence of characters is ASCII Code value , Integer. , So it is divided into integer families
unsigned char
signed char
short
unsigned short [int]
signed short [int]
int
unsigned int
signed int
long
unsigned long [int]
signed long [int]
long long
unsigned long long [int]
signed long long [int]
int a; amount to signed int a; Others are the same ;
and char What is the signed char still unsigned char Standards are undefined , Depends on the implementation of the compiler .
Floating point family :
float // Low precision , The range of stored values is small
double // High precision , The range of stored values is larger
Floating point type can be used as long as it represents decimal
Construction type :
> An array type
> Type of structure struct
> Enumeration type enum
> Joint type union
Custom type —— Create new types by yourself
Pointer types
int *pi;
char *pc;
float* pf;
void* pv;
Empty type :
void Indicates empty type ( No type )
Usually applied to the return type of a function 、 The parameters of the function 、 Pointer types .
give an example :
// first void Indicates that the function will not have a return value
// the second void It means that the function does not need to pass any parameters
void test(void)
{
printf("Hello World!\n");
}
int main()
{
test();
return 0;
}
Two 、 Shaping storage in memory
1. Original code 、 Inverse code 、 Complement code
There are three kinds of integers in the computer 2 Decimal representation , The original code 、 Inverse and complement .
There are three ways of expression Sign bit and Value bits Two parts , The sign bits are all used 0 Express “ just ”, use 1 Express “ negative ”, And the number bit
Positive integer , primary 、 back 、 The complement is the same .
Negative integer , primary 、 back 、 The complement needs to be calculated .
Original code
Directly write the binary sequence in the form of positive and negative numbers to get the original code .
Inverse code
Change the sign bit of the original code , The reverse code can be obtained by inverting other bits in turn .
Complement code
Inverse code +1 You get the complement .
give an example :
int a = 20;
//00000000 00000000 00000000 00010100
int b = -10;
//10000000 00000000 00000000 00001010 -- Original code
//11111111 11111111 11111111 11110101 -- Inverse code
//11111111 11111111 11111111 11110110 -- Complement code
For plastic surgery : Data stored in memory is a binary sequence of complements .
Why? ?
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 .
for example :1-1
2. Introduction to big and small end
What is big end, small end :
Big end ( Storage ) Pattern , The low bit of data is stored in the high address of memory , And the high end of the data , Stored in a low address in memory ;
The small end ( Storage ) Pattern , The low bit of data is stored in the low address of memory , And the high end of the data ,, Stored in a high address in memory .
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 8 bit. 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 .
// Design a small program to determine the current machine byte order
int main()
{
int n = 1;
if (*(char*)&n == 1)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}
3、 ... and 、 Floating point storage in memory
Common floating point numbers :
3.14159
1E10
The family of floating-point numbers includes : float、double、long double type .
The range represented by floating point numbers :float.h In the definition of
1. 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 .
for instance :
Decimal 5.0, Written as binary is 101.0 , amount to 1.01×2^2 .
that , According to the above V The format of , We can draw S=0,M=1.01,E=2.
Decimal -5.0, Written as binary is -101.0 , amount to -1.01×2^2 . that ,S=1,M=1.01,E=2.
IEEE 754 Regulations :
about 32 Floating point number of bits , The highest 1 Bits are sign bits s, And then 8 Bits are exponents E, The rest 23 Bits are significant numbers M.
about 64 Floating point number of bits , The highest 1 Bits are sign bits S, And then 11 Bits are exponents E, The rest 52 Bits are significant numbers M.
IEEE 754 For significant figures M And the index E, There are some special rules .
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 .
As for the index E, The situation is more complicated .
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 numbers , 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 ,2^10 Of E yes 10, So save it as 32 When floating-point numbers are in place , Must be saved as 10+127=137, namely 10001001.
then , Index E Fetching from memory can be further divided into three cases :
E Not all for 0 Or not all of them 1
At this time , 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.
such as :
0.5(1/2) The binary form of is 0.1, Since it is stipulated that the positive part must be 1, That is to move the decimal point to the right 1 position , Then for 1.0*2^(-1), Its order code is -1+127=126, Expressed as 01111110, And the mantissa 1.0 Remove the integer part and make it 0, A filling 0 To 23 position 00000000000000000000000, Then its binary representation is :
0 01111110 00000000000000000000000
E All for 0
At this time , 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 .
E All for 1
At this time , If the significant number M All for 0, Express ± infinity ( It depends on the sign bit s).
边栏推荐
- Detailed explanation of Flink resource management
- cron 表达式周一到周五执行以及只有周六周天执行
- VS2019 16.8 “消失“的团队资源管理器
- lora和lorawan无线技术在物联网的应用
- Free SSL certificate application and deployment practice
- 物联网无线传输技术参数对比表
- AGCO won the 2022 red dot award: design concept award
- 免费SSL证书申请及部署实践
- [batch dos-cmd command - summary and summary] - symbolic link, hard link, soft link, directory link (mklink)
- C语言《通讯录》详解
猜你喜欢

Get to know your NFT again

从小米10发布来看编译优化

Differences between get requests and post requests and usage examples

Airiot low code development platform, 10 minutes to build the Internet of things system

ONNX模型tensor shapes inference和Flops统计工具

PostgreSQL is now installed

Small target detection 1_ Focal loss

El input number disable scientific counting without result, and replace it with El input (type= "number")

Calculate the Euclidean distance between the row vectors of two matrices

Characteristics of Lora technology in wireless communication
随机推荐
Get to know your NFT again
Comprehensive evaluation method
RMAN detailed tutorial (I) -- basic command code
Australia's leading footwear company has partnered with boomi to accelerate the pace of e-commerce and transformation
Full link voltage test: preparations for the test
Enable sandbox function and use in win10
[daily training -- Tencent select 50] 33 Search rotation sort array
async函数实现多个请求处理
Compilation optimization from the release of Xiaomi 10
mysql与idea建立连接出错
Calculate the Euclidean distance between the row vectors of two matrices
Andersen global enters Rwanda
Domestic postman tool, easy to use!
Splash integrates with Acronis to provide scalable remote support
redis集群测试
Minuterie haute performance
CV2. Setmousecallback() displays the pixel value and position of the mouse click image
metaRTC5.0实现webrtc版IPC
【日常训练】515. 在每个树行中找最大值
Onnx model tensor shapes information and flops statistical tools