C language-storage of plastic data


Preface

There are three main points in the storage of plastic data in C language. 1. Original inverse complement 2. Big and small end 3. Shaping truncation and promotion


One, the original anti-complement code

The original code, the inverse code, and the complement code are the binary representation methods of numbers in the computer.

Original code: The highest bit is used as the sign bit (0 means positive, 1 means negative), and the other digital bits represent the absolute value of the value itself.

Inverse code: If it is a positive number, the representation method is the same as the original code; if it is a negative number, the sign bit remains unchanged, and the rest of the bits are inverted, and then the inverse code representation of this number is obtained.

Complement code: If it is a positive number, the expression method is the same as the original code; if it is a negative number, add 1 to the inverse code of the number (equivalent to invert the value of the original code and add 1 to the lowest bit.
Use an eight-digit binary number for example:

Original code One's complement Complement
1 00000001 00000001 00000001
-1 10000001 11111110 11111111

So why does the original anti-complement exist? The reason is that the computer only has an adder, not a subtractor.
In a calculator, all subtraction calculations must be performed by addition. By replacing the subtracted number with the complement of the subtracted number, the subtraction can be transformed into an addition operation.
Try adding the two numbers in the example above.
If you use the original code to calculate:
Insert picture description here
you can see that the answer is -2, but this is not the correct result. Therefore, the clever computer pioneers created the complement code and used the complement code to add: In
Insert picture description here
this way, the calculated answer is correct!

Second, the big and small end

The way computers store data can be divided into big-endian storage and little-endian storage.

The main difference between using the size mode to store data lies in the byte order of storage. The big-endian method stores the high-order bits at the low address, and the little-endian method stores the low-order bits at the low address. Data storage in the big-endian way is in line with normal human thinking, and data storage in the little-endian way is conducive to computer processing. So far, the use of big-endian or little-endian for data storage has not been conclusive.

In a processor system, there may be both big-endian and little-endian modes. This phenomenon has brought a lot of trouble to the software and hardware design of the system. Therefore, it is very important to judge whether the system is big-endian or small-segment storage.
Design a function to judge the large and small ends:

代码一:
void bigorsmall()
{
    
    
int a = 1;
char b = *(char*)&a;
if(b==1)
	printf("小端\n")
else
	printf("大端\n");
}
代码二(利用联合体判断):

int bigorsmall()
{
    
    
	union
	{
    
    	int i;
		char c;
	}un;
	un.i = 1;
	return un.c;
}
如果返回1,则说明是小端机,0则是大端机。

Third, the truncation and promotion of plastic surgery

The truncation and promotion of the shaping are events that may occur when assigning values ​​to the shaping.

1. Shaping truncation

mechanism Triggers when a data type with a large number of bytes is assigned to a data type with a small number of bytes
effect Intercept the low-order data and discard the high-order data

2. Plastic surgery

mechanism Triggers when a data type with a small number of bytes is assigned to a data type with a large number of bytes
effect 1. Unsigned number -> complement 0 to the extra high bit ----------------- 2. Signed number -> complement sign bit

for example

整形截断
int main()
{
    
    
  unsigned char a = 200;
  unsigned char b = 100;
  unsigned char c = 0;
  c = a + b;
  printf(%d ”, c);
  return 0;
}

The output of the above code is 44. The reason is because 200+100=300, but the maximum value of the unsigned char type is 255, and the binary number of 300 is 100101100. At this time, the value passed will be truncated, and the highest 1 is discarded, and the remaining low eight bits are 44. .

整形提升

 int main()  
 {
    
      
    char a = -128;  
    printf("%u\n",a);  
    return 0;  
}

The output result of the above code is as follows
Insert picture description here
. Why is it outputting such a large number here? Let’s convert it to binary to see. The result of converting to binary is 11111111 11111111 11111111 10000000. You can see that the reason for so many 1s should be carried out Fill in.

This is the effect of plastic lifting, let's analyze this code.
Here, the first step of assigning a value is truncated. The binary of -128 is 110000000, and the complement is 110000000. After truncation, it becomes 10000000 (8-bit binary number).

Then when you need to print %u, it must be reshaped to 32 bits. After adding the sign bit, it becomes 11111111 11111111 11111111 10000000, so you can understand why the output result is like this.

Guess you like

Origin blog.csdn.net/weixin_47460769/article/details/113785004