Integer promotion

The order in which we use expression evaluation is partly determined by the precedence and associativity of operators.
Similarly, the operands of some expressions may need to be converted to other types during the evaluation process.

Implicit type conversion

Integer arithmetic operations in the C language are always performed with the precision of the default integer type.
For example,
char a, b, c;
a = b + c;
in the calculation process, the calculation result of b, c is an integer, the value after calculation will obtain the number of bits required by the char type, and the other bits are omitted .

In order to obtain this precision, other types of operands such as characters and short integers in the expression are converted to ordinary int or unsigned int integers before being used. This conversion is called implicit type conversion, that is, integral promotion.

The meaning of integer promotion

The shaping operation of the expression must be executed in the corresponding computing device of the CPU. The byte length of the operand of the integer arithmetic unit (ALU) in the CPU is generally the byte length of int, and it is also the length of the general-purpose register of the CPU-32bit .

Therefore, even if the two char types are added, they must actually be converted to the standard length 32bit of the integer operand in the CPU during execution.

General-purpose CPU (general-purpose CPU) is difficult to directly implement two 8-bit direct addition operations (although there may be such byte addition instructions in machine instructions). Therefore, the various integer values ​​in the expression whose length may be less than the length of int must be converted to int or unsigned int before being sent to the CPU to perform operations.

For example:
char a,b,c;
a = b + c;
The values ​​of b and c are promoted to int integer type, and then the addition operation is performed.
After the addition operation is complete, the result is truncated and then stored in a.
Insert picture description here

How to perform integer promotion?

Integer promotion we can simply divide it into two steps: the
first step:
check the original type of the variable to be promoted by integer?
Step 2:
If it is an integral promotion of an unsigned type, add 0 to the high bit until
it is 32 bits. If it is an integral promotion of a signed type, see what the highest bit (that is, the sign bit) is. If it is 0, add 0 to the high bit until Complement 32 bits, if it is 1 high bit, complement 1 until 32 bits are complemented.

For example:
the integral promotion of
a complex number: char a = -1;
the binary storage of the variable a in the computer is 1111 1111
because char is a signed character type,
so when the integral is promoted, the high bit supplements the sign bit, which is 1.
The result after the promotion is:
1111 1111 1111 1111 1111 1111 1111 1111

Integer promotion for positive numbers
char a = 1;
the binary storage of variable a in the computer is: 0000 0001
because char is a signed character type,
so when the integer is promoted, the high bit supplements the sign bit, which is 0.
The result after promotion is:
0000 0000 0000 0000 0000 0000 0000 0001

Unsigned integral promotion is the same as positive integral promotion.

Instance

We can check several examples to prove that the computer uses integer boost

#include<stdio.h>
int main()
{
    
    
 char a = 0xb6;//0xb6为16进制的182
 short b = 0xb600;
 int c = 0xb6000000;
 if(a == 0xb6)
 printf("a\n");
 if(b == 0xb600)
 printf("b\n");
 if(c == 0xb6000000)
 printf("c\n");
 printf("%u", a);
 printf("%d\n", a);
 system("pause");
 return 0;
}

Insert picture description here
Explain that in the above example, a and b need to be integer promoted, but the bit of c itself meets the CPU calculation requirements without integer promotion, and the number returned after a, b is calculated by the CPU is recognized as a negative number (for example, a is The identification is -74) Naturally, the condition of a == 0xb6 is not satisfied, the result is false, and c is not promoted by integer, the judgment result is true.

#include<stdio.h>
int main()
{
    
    
 char c = 1;
 printf("%u\n", sizeof(c));
 printf("%u\n", sizeof(+c));
 printf("%u\n", sizeof(-c));
 printf("%u\n", sizeof(!c));
 system("pause");
 return 0;
}

Insert picture description here
We can see that as long as the variable c participates in the operation that needs to pass the CPU, the integer promotion will definitely occur, so sizeof© does not pass the CPU operation, and the integer promotion is not required. It is the original char type, which is 1 byte, and The other three are all 4 bytes and perform integer promotion.
Note: !c has also undergone an integral promotion, but it has been specially processed under windows. After running on Linux, it can be found that it is still 4 bytes.
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40893595/article/details/104441826