C language errata-data storage

Shaped storage in memory

For positive numbers: original code=inverse code=complement code

For negative numbers: the highest bit is 1 (representing a negative number), the original code -> the inverse code (the highest bit remains unchanged, and the rest are inverted), the inverse code -> the complement (the last digit of the inverse code plus 1 = complement code)

First look at the following code

#include<stdio.h>

int main(int argv,char *argc[])
{
	char a = -1;
	unsigned char b = -1;
	printf("%d,%d\n",a,b);

    return 0;
}

The print result is: -1, 255

Integers exist in the form of complements in computers .

For char a = -1:

Original code: 1000 0000 0000 0000 0000 0000 0000 0001

Inverse code: 1111 1111 1111 1111 1111 1111 1111 1110

Complement: 1111 1111 1111 1111 1111 1111 1111 1111

For char, take out the lower eight bits 1 111 1111, and the data is printed in %d, which needs to be upgraded to 32 bits. For signed, the complemented data is determined according to the upper sign bit

Complemented code ( according to the data type ---char a ) 1111 1111 1111 1111 1111 1111 1111 1111 ----------> The original code after completion ( according to the type of printing: if% d, the most significant bit is complemented according to the type, if %u, the original complement is the same ) 1000 0000 0000 0000 0000 0000 0000 0001 --------------->Print -1 (stored is the complement , The original code is printed)

For unsigned char b = -1:

Original code: 1000 0000 0000 0000 0000 0000 0000 0001

Inverse code: 1111 1111 1111 1111 1111 1111 1111 1110

Complement: 1111 1111 1111 1111 1111 1111 1111 1111

For unsigned char, take out the low eight bits 1 111 1111, and print the data in %d, which needs to be upgraded to 32 bits. For unsigned char , the high bits are filled with 0

The complement of the complement 000 0000 0000 0000 0000 0000 1111 1111 (for unsigned, it is a positive number, and its original complement’s complement is the same) ------------ --->Print 255 (stored the complement code, print the original code)

Look at the following code

#include<stdio.h>

int main(int argv,char *argc[])
{
	char a = -128;

	printf("%u\n",a);

    return 0;
}

Printing result: 4294967168

For char a = -128:

Original code : 1000 0000 0000 0000 0000 0000 1000 0000

Inverse code: 1111 1111 1111 1111 1111 1111 0111 1111

Complement: 1111 1111 1111 1111 1111 1111 1000 0000

Take out the lower eight digits 1000 0000 %u is an unsigned positive number to print

The complement is increased to 32 bits (because a is a signed type, it needs to be complemented according to the highest bit type): 1111 1111 1111 1111 1111 1111 1000 0000 (according to %u) ----> output 4294967168

 

Storage of floating-point type in memory

For the integers mentioned before, the size can be viewed in limits.h, while the floating-point data size range can be viewed in float.h

First look at the following code:


#include<stdio.h>
#include <arpa/inet.h>


int main(int argv,char *argc[])
{
	int a = 5;
	float *p = (float*)&a;

	printf("a =%d\n",a);
	printf("*p =%f\n",*p);

	*p = 5.0;
	printf("a =%d\n",a);
	printf("*p =%f\n",*p);



    return 0;

}

Output result:

The data storage has changed during the coercion of an integer to a floating point number

So how is floating-point data stored?

Example: Single-precision floating-point number 5.5  

       Binary representation: 101.1 ----------5 (101) .5 (0.5*2 = 1.0 is just 1 for details: https://jingyan.baidu.com/article/eb9f7b6dc692e9c79264e878.html )

      101.1 = (-1)* 0 * 1.011 * 2^2

      s=0 M=1.011 (only .011 is actually stored) E=2

       

         

      For single-precision floating point number S: 1 bit E: 8 bit M: 23 bit        

                                     S=0 M=1.011 (only .011 is actually stored) E=2(2+127) Stored value: 129 Real value: 2

                                     0 1000 0001 011 0000000000000.....(After conversion, fill in the binary number after the decimal point, and add 0 to the remaining digits)

           

   After reading this, everyone must know why the above results are output.

  int a=5   

Original=inverse=complement: 0000 0000  0000 0000 0000 0000 0000 0101 when we *p use %f to read this complement     

E is all 0, which means a very small number close to 0, so *p =0.000000

    When stored in floating point type 2+127

5.0 = 101.0 = (-1)*0 * 1.01*2^2  =  01000 0001 0100 0000 0000 0000 0000 000

When we read in %d, it becomes 0100 0000 1010 0000 0000 0000 0000 0000 = 1084227584 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_45604814/article/details/112388101