当前位置:网站首页>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. 结语
兄弟们,记住了!如果觉得写的不错,来个一键三连呗!!!!!!!!!!!!!!!
边栏推荐
- 接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
- 4D Summary: 38 Knowledge Points of Distributed Systems
- APP自动化测试框架-UiAutomator2基础入门
- 如何让您的公司内容满足 GDPR 合规性
- Synchronization lock synchronized traces the source
- mysql 找不到或无法加载已注册的 .Net Framework Data Provider。
- AI识万物:从0搭建和部署手语识别系统
- TF使用constant生成数据
- mysql multi-table left link query
- 角度和弧度的相互换算
猜你喜欢

在“企业通讯录”的盲区,融云的边界与分寸

线段相交的应用

The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
![[Cloud Native] 4.2 DevOps Lectures](/img/d0/2cd32351234401fdfecd3a829a639d.png)
[Cloud Native] 4.2 DevOps Lectures

“稚晖君”为2022昇腾AI创新大赛打call 期待广大开发者加入

Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)

几种绘制时间线图的方法

腾讯继续挥舞降本增效“大刀”,外包员工免费餐饮福利被砍了

APP自动化测试框架-UiAutomator2基础入门

【双链表增删查改接口的实现】
随机推荐
面试官:Redis 大 key 要如何处理?
LeetCode26: remove duplicates in sorted array
“稚晖君”为2022昇腾AI创新大赛打call 期待广大开发者加入
JS解混淆-AST还原案例
编程语言中,取余和取模的区别
navicat 快捷键
一本通2074:【21CSPJ普及组】分糖果(candy)
AI识万物:从0搭建和部署手语识别系统
np中的round函数,ceil函数与floor函数
AI+医疗:使用神经网络进行医学影像识别分析
从产品角度看 L2 应用:为什么说这是一个游乐场?
聊聊SQL语句中 DDL 、DML 、DQL 、DCL 分别是什么
Several ways to draw timeline diagrams
小黑leetcode之旅:94. 二叉树的中序遍历(补充Morris 中序遍历)
小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
Rust 解引用
【云原生】4.2 DevOps 精讲篇
CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source
Deceptive Dice(期望计算)
AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero