Integer promotion and arithmetic conversion you don't know

catalogue

1. Integer lift

1. What is integer lifting?

2. Significance of integer improvement

3. How to improve integer? (dangerous)

(1) Integer lift

(2) sizeof instance

(3) Calculation example (process of computer operation)

2. Arithmetic conversion

3. There are exercises related to integer lifting and arithmetic conversion

1. Integer lift

1. What is integer lifting?

The integer arithmetic operation of C is always performed at least with the precision of the default integer type. To achieve this precision, the characters and short operands in the expression are converted to normal integers before use. This conversion is called integer promotion.

2. Significance of integer improvement

The integer operation of the expression shall be executed in the corresponding operation device of the CPU. The byte length of the operand of the integer operator (ALU) in the CPU is generally the byte length of int and the length of the general register of the CPU. Therefore, even if the two char types are added, they actually have to be converted to the standard length of integer operands in the CPU when executed by the CPU. General purpose CPU (general purpose CPU) is difficult to directly realize the direct addition of two 8-bit bytes (although there may be such byte addition instructions in machine instructions). Therefore, all integer values whose length may be less than int in the expression must be converted to int or unsigned int before they can be sent to the CPU for operation.

Regardless of any bit operator, the target is to be calculated by the computer, and only the CPU in the computer has the computing power (understood simply first), but the calculated data is in memory. Therefore, before calculation (regardless of any operation), you must get the data from the memory to the CPU. Where do you get it to the CPU? There is no doubt that in the CPU register. The number of bits of the register itself varies with the number of bits of the computer. Generally, under 32 bits, the number of bits of the register is 32 bits. However, your char type data has only 8 bits. When read into the register, only the lower 8 bits can be filled. To sum up, the data of operations performed in the computer are integer, and the results of sizeof have been produced during compilation, not during operation.

3. How to improve integer? (dangerous)

(1) Integer lift

Integer promotion is promoted according to the sign bit of the data type of the variable.

Here are some examples:

//Shaping and lifting of negative numbers
char c1 = -1;
//There are only 8 bits in the binary bit (complement) of variable c1:
//1111111
//Because char is a signed char
//Therefore, when shaping and lifting, the high supplementary symbol bit is 1
//The result of the promotion is:
//11111111111111111111111111111111
//Positive integer lifting
char c2 = 1;
//There are only 8 bits in the binary bit (complement) of variable c2:
//00000001
//Because char is a signed char
//Therefore, when shaping and lifting, the high supplementary symbol bit is 0
//The result of the promotion is:
//00000000000000000000000000000001
//Unsigned shaping lifting, high complement 0

(2) sizeof instance

#include<stdio.h>
int main()
{
	char c = 0;
	printf("%d\n", sizeof(c));//1
	printf("%d\n", sizeof(~c));//4
	printf("%d\n", sizeof(c<<1));//4
	printf("%d\n", sizeof(c>>1));//4
   printf("%d\n",sizeof(+c));//4
   printf("%d\n",sizeof(-c));//4
   //The above examples are involved in the operation, that is, integer promotion occurs, so the result will change
   //That is, as long as short and short variables participate in the operation, integer promotion will occur
   printf("%d\n",sizeof(!c));//1 under vs, 4 under Linux, special cases
	return 0;
}

Supplementary knowledge points: two attributes of expression

int main()
{
    int a = 10;
    int b = 20;
    a + b;//The expression has two properties: value property and type property
    //30 is the value attribute
    //int is the type attribute, which can be derived without calculation and can be deduced by the computer
    //sizeof() in the above code block uses the type attribute of the expression and is not evaluated
}

Conclusion: the expression inside the sizeof() bracket does not participate in the operation. The computer only automatically deduces its type and obtains the answer. Moreover, this process is not carried out in the running stage, but in the compilation stage (as can be proved by the following example)

int main()
{
    short s = 20;
    int a = 5;
    printf("%d\n",sizeof(s = a + 4));//The print result is 2. Integer promotion occurs, but truncation occurs during storage in s
    //That is, the final type is s, and who has the final say in the final outcome?
    printf("%d\n",s);//The print result is 20. It can be concluded that the expression inside sizeof will not really participate in the operation
    //Because sizeof has been executed at the compilation stage, that is, sizeof(s = a + 4) has been replaced by 2 after compilation
    //Therefore, the code s = a + 4 will not be run during operation, so s will not be changed
    return 0;
}

(3) Calculation example (process of computer operation)

int main()
{
    char a = 5;
    //5 -- 0000000000000000000101 complement of integer 5
    //5 - > truncation occurs in char a, that is, the data finally stored in a is 00000101
    char b = 126;
    //126--00000000000000000000000001111110
    //126 - > truncation occurs in char b, that is, the final stored data is 01111110
    char c = a + b;
    //When a and b are added, a and b are both char types
    //Integer promotion occurs when expressions are evaluated
    //A -- 00000101 (the highest bit is the sign bit, and the sign bit of a is 0, so all the preceding bits are supplemented with 0)
    //a--00000000000000000000000000000101
    //b -- 01111110 (the highest bit is the sign bit, and the sign bit of b is 0, so all the preceding bits are supplemented with 0)
    //b--00000000000000000000000001111110
    //a+b--00000000000000000000000010000011
    //Store the value of a+b into char c and truncation occurs. At this time, C -- 10000001 (complement)
    printf("%d\n",c);//Q: what is the output of c at this time?
    //When printing as an integer, integer promotion occurs again
    //C -- 11111111111111111111111110000011 (complement)
    //C -- 1000000000000001111100 (inverse code)
    //C -- 1000000000000001111101 (original code) - (- 125)
    //When you see in an expression that the memory occupied by a variable does not reach the size of four bytes occupied by an integer, you must first promote it to
    //Integer. In the above expression, when a and b are char types, it is 1 byte. First, it is promoted to integer
    return 0;
}

2. Arithmetic conversion

If the operands of an operator are of different types, the operation cannot be performed unless one of the operands is converted to the type of the other operand. The following hierarchy is called ordinary arithmetic conversion.

long double

double

float

unsigned long int

long int

unsigned int

int

Note: conversion from bottom to top

If the type of an operand is low in the above list, it must be converted to the type of another operand before performing the operation. (i.e. the following types are converted upward)

Note: the arithmetic conversion should be reasonable, otherwise there will be some potential problems.

3. There are exercises related to integer lifting and arithmetic conversion

Many partners ask why there are no matching exercises. In fact, they have already talked about it before, so I won't repeat it here. Next, I'll send you the link. After reading these knowledge points, you should consolidate them in time!

Related supporting exercises

Tags: C Back-end

Posted on Thu, 02 Dec 2021 19:06:03 -0500 by webing