Analysis of storage methods of integer and floating point types

This article will analyze the access methods of integer and floating-point types in memory in C. All the following experiments are carried out in a 32-bit environment.

1. Classification of integer and floating point types

1.1. Integer

Integer data include: char, short, int, long, which can be further classified according to whether it is signed or not.

1.2, floating point type

Floating-point data include: float and double data are floating-point data

Second, the storage of integers in memory

      The data types of different variables are different, and the space opened up in memory according to their data types is also different. For example, the space size of int type data in memory is 4 bytes, and the space size of char type data in memory is 1 byte.

      And integer data is stored in the form of complement code in the memory, because in the computer system, the use of complement code can unify the sign and value fields, and at the same time make the addition and subtraction inside the computer can be processed uniformly, which is It solves that the CPU only has an adder but can do subtraction with all its heart. And the operation process of complement code and original code is the same, no additional hardware circuit is needed.

2.1, big endian and little endian storage

In the computer, it is all in bytes, and each address unit corresponds to one byte. In the C language, except for the data of the char type which is 1 byte, other data types are larger than 1 byte (different compilers have differences), so for processors with more than 1 byte, there must be how The problem of arranging multiple bytes, as shown in the following figure:

Store data 0x1122 at the address of the first address bit 0x0010, there are the following two sequential ways of storing data

 These two storage methods are big-endian and little-endian respectively.

Programmatically check whether the compiler is big-endian or little-endian:

#include <stdio.h>
int main()
{
	int a = 1;
	char* b = (char*)&a;
	if (*b == 1)
	{
		printf("小端\n");
	}
	else
	{
		printf("小端\n");
	}
	return 0;
}

In memory, the storage location of a is as follows:

Convert &a to char* type and assign it to b. Since a is an integer type, the data allocated for a occupies 4 bytes. After the type is converted and assigned to b, since b is a character pointer, when b is dereferenced Only use the lowest byte space among the four byte spaces opened for a, so if it is little endian, *b==1; if it is big endian, *b==0. In this way, it can be judged whether it is big-endian storage or little-endian storage.
3. Storage of floating point numbers in the computer
The storage of floating-point numbers in the computer is quite different from the storage of integers.
Unlike integers, floating-point numbers include order symbols and mantissas in addition to positive and negative numbers, which can be expressed in the following form:
                                                     (-1)^S*M*2^E
 S is a number symbol, M is a mantissa, and E is an exponent code.
For example, 5.5 is expressed as a floating point number as follows:
5.5 (decimal) = 101.1 (binary)
101.1=(-1)^0*1.011*2^2
Therefore, change 5.5 to a floating-point representation, the number symbol S=0, the mantissa M=1.011, and the exponent code E=2.
At the same time, depending on whether it is in a 32-bit environment or a 64-bit environment, the precision of floating-point numbers and the range of values ​​​​are different.
The following is the value range of the floating-point number symbol, mantissa and exponent in the 32-bit environment:

 

The following are the value ranges of floating-point numbers, mantissas, and exponents in a 64-bit environment:

3.1. Regulations in floating-point numbers

1 <= M < 2 : M can be written in the form of 1.XXXXXX, where xxxxxxxx represents the decimal part. Since the default integer part is 1, the 1 in the integer part will not be stored but only the decimal will be stored when storing part.

E :

As for the index E , the situation is more complicated.
First, E is an unsigned integer ( unsigned int ), which means that if E is 8 bits, its value range is 0~255 ; if E is 11 bits, its value range is 0~2047 . However, we know that E in scientific notation can have negative numbers, so IEEE 754 stipulates that an intermediate number must be added to the real value of E when stored in memory . For 8 -bit E , this intermediate number
is 127 ; for 11 -bit E , this intermediate number is 1023 .
For example, the E of 2^10 is 10 , so when saving it as a 32- bit floating point number, it must be saved as 10+127=137 , that is
10001001

3.2. Three cases of floating-point number storage

  3.2.1. E is not all 0 or not all 1

This situation is the storage of general floating-point numbers in the computer,

At this time, the floating-point number is represented by the following rules, that is, the calculated value of the exponent E is subtracted by 127 (or 1023 ) to obtain the real value, and then the
Add the first digit 1 before the significant digit M.
for example:
The binary form of 0.5 ( 1/2 ) is 0.1 , since it is stipulated that the positive part must be 1 , that is, the decimal point is shifted to the right by 1 , then it is
1.0*2^(-1) , its order code is -1+127=126 , expressed as
01111110 , and the mantissa 1.0 removes the integer part to be 0 , fills 0 to 23 digits 00000000000000000000000 , then its binary
The representation form is :
0 01111110 00000000000000000000000
3.2.2, E is all 0
E is all 0, which means that the index at this time is the smallest negative integer within a range, and the floating-point number represented is infinitely close to 0.
At this time, the exponent E of the floating point number is equal to 1-127 (or 1-1023 ) which is the real value, and the effective number M is no longer added with the first 1 , but is reduced to a decimal of 0.xxxxxx . This is done to represent ±0 , as well as close to
0 is a very small number.
3.2.3, E is all 1
At this time, if the significant number M is all 0 , it means ± infinity (positive or negative depends on the sign bit s )

Guess you like

Origin blog.csdn.net/peng_lv/article/details/128585068