C language - integer promotion

Table of contents

What is integer promotion? Why integer promotion?

Points to note about integer promotion

the code


What is integer promotion? Why integer promotion?

The integer operation of the expression must be executed in the corresponding operation device of the CPU. The byte 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. Therefore, even if the addition of two char types is actually performed by the CPU, it must first be converted to the standard length of the integer operand in the CPU. It is difficult for a general-purpose CPU to directly add two 8-bit bytes (although there may be such byte addition instructions in machine instructions). Therefore, the various integer values ​​in the expression whose length is less than the length of int must be converted into int or unsigned int before being sent to the CPU to perform operations.


Points to note about integer promotion

The shaping promotion of negative numbers, the high bit is complemented with 1 (complement sign bit)
The shaping promotion of positive numbers, the high bit is complemented with 0 (complement sign bit)
 

When the unsigned integer is promoted, the high bit is filled with 0


Code and Examples

Example 1:

#include<stdio.h>
int main()
{
	//char——>unsigned char(有符号dchar)
	char a = 3;
	//四个字节的数据非要放到1个字节的空间里面叫做截断
	//00000000000000000000000000000011(32个比特位)
	//00000011->a(只能存放8个比特位到a里)
	
	char b = 127;
	//00000000000000000000000001111111
	//01111111->b
	
	char c = a + b;
	//整形提升
	//00000000000000000000000000000011//最高位的符号位是“0”,就补“0”
	//00000000000000000000000001111111
	//00000000000000000000000010000010//相加后
	//10000010->c//截断

	printf("%d\n", c);
		//%d打印十进制的整数
		//还是要继续整型提升
		//11111111111111111111111110000010——补码//最高位的符号位是“1”,就补“1”
		//11111111111111111111111110000001(减一)
		//00000000000000000000000001111110(取反)——原码
		//-126
	return 0;
}

 Example 2:

#include<stdio.h>
int main()
{char a=-1;
//10000000000000000000000000000001——原码
//11111111111111111111111111111110——反码
//11111111111111111111111111111111——补码
//11111111——截断
printf("%d",a);
//整型提升,补最高位
//11111111111111111111111111111111——原码
//10000000000000000000000000000001——补码
//打印出来是-1
return 0;
}

Example three:

#include<stdio.h>
int main()
{
	char a = -1;
	//10000000000000000000000000000001——原码
	//11111111111111111111111111111110——反码
	//11111111111111111111111111111111——补码
	//放到char里面要截断
	//11111111
	//printf打印%d时,要整型提升
	//有符号数补符号位
	//11111111111111111111111111111111——补码
	//10000000000000000000000000000001——原码
	//所以a打出来是-1
	signed char b = -1;
	//在vs里面char就是signed char
	//所以b打出来也是-1
	
    unsigned char c = -1;
	//10000000000000000000000000000001——原码
	//11111111111111111111111111111110——反码
	//11111111111111111111111111111111——补码
	//放到char里面要截断
	//11111111
	//printf打印%d时,要整型提升
	//无符号数补0
	//00000000000000000000000011111111——补码
	//00000000000000000000000011111111——反码
	//00000000000000000000000011111111——原码
	printf("a=%d,b=%d,c=%d", a, b, c);
	return 0;
}

Example four:

#include<stdio.h>
int main()
{
	char a = -128;
	//10000000000000000000000010000000——原码
	//11111111111111111111111101111111——反码
	//11111111111111111111111110000000——补码
	//10000000截断
	//11111111111111111111111110000000——补码、反码、补码
	printf("%u\n", a);
	return 0;
}

Guess you like

Origin blog.csdn.net/outdated_socks/article/details/129414266