当前位置:网站首页>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. 结语
兄弟们,记住了!如果觉得写的不错,来个一键三连呗!!!!!!!!!!!!!!!
边栏推荐
- CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source
- 必看设计干货|易知微设计师是怎么做标准可视化设计服务的?
- 2022 首期线下 Workshop!面向应用开发者们的数据应用体验日来了 | TiDB Workshop Day
- mysql multi-table left link query
- APP自动化测试框架-UiAutomator2基础入门
- STC8H开发(十五): GPIO驱动Ci24R1无线模块
- AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
- MySQL:错误1153(08S01):得到的数据包大于“ max_allowed_packet”字节
- 深度剖析 Apache EventMesh 云原生分布式事件驱动架构
- 好未来,想成为第二个新东方
猜你喜欢
Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
深度剖析 Apache EventMesh 云原生分布式事件驱动架构
Synchronization lock synchronized traces the source
《强化学习周刊》第57期:DL-DRL、FedDRL & Deep VULMAN
【云原生】4.2 DevOps 精讲篇
MySQL慢查询的多个原因
从源码方面来分析Fragment管理中 Add() 方法
The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
AI+医疗:使用神经网络进行医学影像识别分析
Flask入门学习教程
随机推荐
SecureCRT 设置超时自动断开连接时长
The round functions in the np, ceil function and floor function
Technology Sharing | How to Handle Header Cookies in Interface Automation Testing
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
Bean life cycle
从源码方面来分析Fragment管理中 Add() 方法
POWER SOURCE ETA埃塔电源维修FHG24SX-U概述
小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
Pagoda measurement - building LightPicture open source map bed system
面试官:Redis 大 key 要如何处理?
“稚晖君”为2022昇腾AI创新大赛打call 期待广大开发者加入
4D Summary: 38 Knowledge Points of Distributed Systems
AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero
Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
Deceptive Dice(期望计算)
CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source
Bean生命周期
台风生成,广州公交站场积极开展台风防御安全隐患排查
Use convert_to_tensor in Tensorflow to specify the type of data