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

C language - data storage

2022-06-11 12:44:00 Xiaolock830

**

data type

**

char     // Character data type 
short    // Short 
int      // plastic 
long     // Long integer 
long long// Longer plastic surgery 
float    // Single-precision floating-point 
double   // Double precision floating point 

The memory size of these data types can be found in my first blog , The preceding types all have corresponding unsigned The type and signed type , That is, signed type and unsigned type , In theory, unsigned ones can store larger numbers .
If there's no unsigned, Then the default is signed .

char
    unsigned char
    signed char
short
    unsigned short [int]
    signed short [int]
int
unsigned int
    signed int
long
    unsigned long [int]
    signed long [int]

Empty type :

void Indicates empty type

Usually applied to the return type of a function 、 The parameters of the function 、 Pointer types .

**

Plastic storage :

**

Original code 、 Inverse code 、 Complement code :

Original code
Directly translate binary into binary in the form of positive and negative numbers

Inverse code
Change the sign bit of the original code , The other bits can be inverted in turn .

Complement code
Inverse code +1 You get the complement .

A positive number 、 back 、 The complement is the same .
For plastic surgery : Data stored in memory is actually stored in the complement .

Why is data stored as a complement :

In computer system , All values are represented and stored by complements . The reason lies in , Use complement , You can combine sign bits and numeric fields One processing ;

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 It's the same , No need for additional hardware circuits .

Big end and 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 .

Judge the size of the machine :

#include <stdio.h> 
int check_sys()
{
    
	int i = 1;
    return (*(char *)&i);
}
int main() 
{
    
    int ret = check_sys();
    if(ret == 1)
    {
    
		printf(" The small end \n"); }
	else
	{
    
		printf(" Big end \n");
	}
	return 0; 
}

Floating point storage :

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 example, 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.

In memory , This is how we allocate floating point numbers :
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.

We can find out , Write the above form for any number ,M First of all 1, So ignore this when saving to memory 1.
And for less than 1 Number of numbers ,E The storage of may be negative , The storage range will be reduced by half due to the storage of negative numbers , So we E Change to an unsigned positive number , When storing , If it is 8 Bit E You need to add 127,11 Bit plus 1023

原网站

版权声明
本文为[Xiaolock830]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012125433339.html