Data storage and related typical examples include detailed explanations of floating-point number storage in memory

Data storage example

example one

#include <stdio.h>
int main()
{
    
    
	int a = -20;
	unsigned int b = 10;
	printf("%d", a + b);
	return 0;
}

-20 is stored in a, and stored in the complement code. When calculating a+b, a will be arithmetically converted to unsigned int, but for a, even if it is converted to unsigned int, the value stored in a will not change, because the value of a The space is still four bytes, and it should be printed in the form of %d when printing, so it needs to print its complement, and finally it can be calculated as -10. If it is changed to %u for printing, it is regarded as a positive number, that is The original code is the complement code, which will print out a super large number

Example 2

#include <windows.h>
int main()
{
    
    
	unsigned int i;
	for (i = 9; i >= 0; i--)
	{
    
    
		printf("%u\n", i);
		Sleep(1000);

	}
	return 0;
}

Since i is of unsigned int type, when i is a negative number, the complement is still stored, and its complement is stored in i. From the perspective of i, i will store a super large
number, for example- 1. Standing at the angle of i, the stored value is all 1. If it is printed with %u, it will print all 1, but if it is printed with %d, the complement code will be converted into the original code, and -1 will be printed, but when i>0 is judged condition, i is still a very large number compared with 0, i is always greater than 0, so there will be an infinite loop

Example three

int main()
{
    
    
char a[1000];
int i;
for(i=0; i<1000; i++)
{
    
    
a[i] = -1-i;
}
printf("%d",strlen(a));
return 0;
}

Since the range of the char type is -128~127, it
is gradually stored in the array a starting from -1. When it is stored to -128, i++ will become 127, and then gradually decrease to 0, and the end symbol of the string is '\ 0', its ascii value is 0, that is, when encountering 0, there are 255 numbers in total, and the output is 255

Example four

#include <stdio.h>
unsigned char i = 0;
int main()
{
    
    
for(i = 0;i<=255;i++)
{
    
    
printf("hello world\n");
}
return 0;
}

The range of i is 0~255. When 256 is stored in i, the eight digits after interception will be all 0, so there will be an infinite loop

Storage of floating point numbers in memory

Floating-point numbers are not stored like integers. It has a special storage method.
Any floating-point number V can be written in the following way
(-1)^S * M * 2 ^E
. When S is 0, V is a positive number and S is When 1, V is a negative number, M represents a significant number, and E represents an exponent.
For example, 5.5 can be written as (-1)^0 * 1.011 * 2 ^2
Sign bit S: 0
Significant number M: 1.011
Exponent bit E: 2
For float For the type, there are 32 bits, 1 bit is the sign bit, 8 bits are the exponent bits, and the remaining 23 bits are valid numbers. For the double type, there are 64
bits, 1 bit is the sign bit, and 11 bits It is the exponent bit, and the remaining 52 bits are the effective number.
The S bit stores 0 or 1, which means positive or negative. The E index bit only stores the exponent. M can be written as 1.xxxxxx, so the number after the decimal point is stored and output at that
time The missing value will be automatically filled back
, but some floating-point numbers cannot be saved accurately. For example, if 5.3 is stored in a float type, the stored value is 5.30000019, because the 3 after the decimal point in 5.3 is always converted to binary. Only dots can be combined into 3, and the effective number can only store 23 bits, so there will be errors.
For storing the exponent E, E is an unsigned integer.
The value range of the float type E is 0~255. For the double type E The range of values ​​is 0 ~ 2^11 - 1, but if 0.5 is stored,
it will be converted into (-1)^0 1.0 (2 ^ -1), and E is -1, so for 8-digit E, The actual value stored in E must be added with an intermediate number 127. For 11-digit E, the actual value of E must be added with 1023.
For withdrawing the exponent E, there are three cases
1. E is not all 0 or not all 1.
Subtract 127 or 1023 from the value of E to get the real exponent. 2.
When E is all 0,
it means +/-1.xxxxx 2^-127, which tends to 0 Numbers, at this time we directly analyze E as 1-127 (1-1023), and M is restored to 0.xxxx
3. When E is all 1
, it means +/- 1.xxxxxx
2^128, which is a very large the numbers

Typical example of floating-point memory storage

int main()
{
    
    
int n = 9;
float *pFloat = (float *)&n;
printf("n的值为:%d\n",n);
printf("*pFloat的值为:%f\n",*pFloat);
*pFloat = 9.0;
printf("num的值为:%d\n",n);
printf("*pFloat的值为:%f\n",*pFloat);
return 0;
}

Store 9 from the perspective of plastic, the original inverse complement code is 0000000000000000000000000001001
If you take it out from the perspective of float type, S:-1, M: 000000000000000000001001, E: 00000000, so the index is directly restored to 1-127, and M is 0.0000000 0000000000001001, So it will print 9,0.000000.
For storing 9.0 in n from the perspective of floating point numbers, 9 is converted into (-1)^0 1.001 2 ^3 and stored in memory is 0
00000011 00100000000000000000000.
Take it out, it is a very large number
. Take it out from the perspective of float type, then restore it back, (-1)^0 1.001 2 ^3, the printed value is 9.000000

Guess you like

Origin blog.csdn.net/wan__xia/article/details/129174264