当前位置:网站首页>This article lets you quickly understand implicit type conversion [integral promotion]!
This article lets you quickly understand implicit type conversion [integral promotion]!
2022-08-09 23:43:00 【A programmer who wants to punch】
0.隐式类型转换[整型提升]
C的整型算术运算总是至少以缺省整型的精度来进行的.为了获得这个精度,表达式中的字符和短整型操作数在使用之前被转换为普通整型,这种转换称为整形提升.简单来说就是,少于4The type operation of a byte will be automatically converted according to certain rulesint或者unsigned int进行运算.
char a,b,c;
a = b + c;
//在进行b+c运算时,因为b和c都为char类型,一个字节,
//所以b和cwill be promoted to ordinary integer,然后再执行加法运算.
//加法运算完成之后,结果将被截断(从后往前截取4个bit位),然后再存储于a中.
1.如何进行整形提升呢?
Integer promotion is based on the data type of the variable符号位来提升的.
1.1 负数的整形提升【高位补符号位】
char c = -1:
//Data exists in memory in two's complement form!

变量c的二进制位(补码)中只有8个bit位:11111111,因为char为有符号的char ,So when it comes to shaping,高位补充符号位,补充1,The result bit after shaping and boosting11111111111111111111111111111111.
1.2 正数的整形提升【高位补符号位】
char c = 1;

变量c的二进制位(补码)中只有8个bit位:00000001,因为char为有符号的char ,So when it comes to shaping,高位补充符号位,补充0,The result bit after shaping and boosting00000000000000000000000000000001.
1.3无符号整形提升,高位补0
unsigned char c = -1;

变量c的二进制位(补码)中只有8个bit位:11111111,因为char为无符号的char ,So when it comes to shaping,高位补充0,The result bit after shaping and boosting00000000000000000000000011111111.
2 整形提升的例子
2.1 例1
int main()
{
char a = 0xb6;
short b = 0xb600;
int c = 0xb6000000;
if(a==0xb6)
printf("a");
if(b==0xb600)
printf("b");
if(c==0xb6000000)
printf("c");
return 0;
}
这个程序的结果是什么呢!很明显是:c
因为a、b要进行整型提升,但是c不需要整型提升.
2.2 例2
int main()
{
char c = 1;
printf("%u\n",sizeof(c)); //1
printf("%u\n",sizeof(+c));//4
printf("%u\n",sizeof(-c));//4
return 0;
}
cJust participate in the operation of the expression,就会发生整形提升,所以只有sizeof(c)为1个字节,Others have undergone plastic lifting,变成了4个字节.
3. 结语
兄弟们,记住了!如果觉得写的不错,来个一键三连呗!!!!!!!!!!!!!!!
边栏推荐
猜你喜欢

AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis

2022 首期线下 Workshop!面向应用开发者们的数据应用体验日来了 | TiDB Workshop Day

上海控安SmartRocket系列产品推介(三):SmartRocket iVerifier计算机联锁系统验证工具

Sudoku | Backtrack-7

《强化学习周刊》第57期:DL-DRL、FedDRL & Deep VULMAN

STC8H Development (15): GPIO Drives Ci24R1 Wireless Module

JSON 基本使用

几种绘制时间线图的方法

Install win virtual machine on VMware
6 rules to sanitize your code
随机推荐
【云原生】4.2 DevOps 精讲篇
Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
SecureCRT 设置超时自动断开连接时长
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
编译原理之文法
《强化学习周刊》第57期:DL-DRL、FedDRL & Deep VULMAN
Simple questions peek into mathematics
任务流执行器是如何工作的?
【EF】 更新条目时出错。有关详细信息,请参见内部异常。[通俗易懂]
从产品角度看 L2 应用:为什么说这是一个游乐场?
简单问题窥见数学
TF uses constant to generate data
五星控股汪建国:以“植物精神”深耕赛道,用“动物精神”推动成长
[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
Tensorflow中使用convert_to_tensor去指定数据的类型
宝塔实测-搭建LightPicture开源图床系统
Use zeros(), ones(), fill() methods to generate data in TF
万字总结:分布式系统的38个知识点
L3-2 Delete up to three characters (30 points)
金山云地震,震源在字节?