当前位置:网站首页>小小笔记-整型提升(C语言)
小小笔记-整型提升(C语言)
2022-06-30 17:58:00 【风吹电风扇】
写在前面
总算把学校的实训搞完了,现在开始正常更新。
大家好,这里是风扇的小小笔记,本篇笔记简单记录了整形提升的概念、意义以及简单的示例,感谢各位的支持。
整形提升
- 整形提升概念
C的整型算术运算总是至少以缺省整型类型(int)的精度来进行的。为了达到这个精度,表达式中的字符型和短整型操作数在使用之前被转换为普通整型,这种转换称为整型
提升。
- 整形提升意义
表达式的整型运算要在CPU的相应运算器件内执行,CPU内整型运算器(ALU)的操作数的字节长度
一般就是int的字节长度,同时也是CPU的通用寄存器的长度。
因此,即使两个char类型的相加,在CPU执行时实际上也要先转换为CPU内整型操作数的标准长
度。
通用CPU(general-purpose CPU)是难以直接实现两个8比特字节直接相加运算(虽然机器指令
中可能有这种字节相加指令)。所以,表达式中各种长度可能小于int长度的整型值,都必须先转
换为int或unsigned int,然后才能送入CPU去执行运算。
- 整形提升规则
️整形提升时高位补充符号位,无符号类型则高位补0.
- 例子:
以int型和unsigned int型来打印char型数据的过程
int main()
{
char a = -128;
//char 范围-128—127
printf("%d %u",a, a);
return 0;
}
运行结果:
a的值的储存过程:
10000000 00000000 00000000 10000000 -128的原码
原码取反
11111111 11111111 11111111 01111111 -128的反码
反码加一
11111111 11111111 11111111 10000000 -128的补码
a的长度位1字节 a:10000000 补码截断,取最后一个字节
取出a的值:
10000000 a
11111111 11111111 11111111 10000000 a整形提升
补码减一
11111111 11111111 11111111 01111111 提升后的a的反码
符号位不变,其他位取反
10000000 00000000 00000000 10000000 提升之后a的原码
以int形式打印a,最高位为符号位
打印的值为-128
以unsigned int形式打印:最高位不是符号位
打印的值为4294967168
最后
以上位本篇笔记的全部内容,原码、反码和补码在上一篇文章讲过了故这次就讲的比较粗略。
最后感谢各位对小风扇的支持,一起努力啊!
边栏推荐
- How to improve the three passive situations in data analysis
- 「干货」数据分析常用的10种统计学方法,附上重点应用场景
- 屏幕显示技术进化史
- 音频 librosa 库 与 torchaudio 库中 的 Mel- spectrogram 进行对比
- 充值满赠,IM+RTC+X 全通信服务「回馈季」开启
- 小球大小随机,随机运动碰撞
- Lenovo Yoga 27 2022, full upgrade of super configuration
- 全栈代码测试覆盖率及用例发现系统的建设和实践
- Nodejs installation and introduction
- 《Go题库·15》go struct 能不能比较?
猜你喜欢

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

Evolution of screen display technology

屏幕显示技术进化史

VMware16安装Win11虚拟机(最全步骤+踩坑)

Detailed single case mode

Four tips tell you how to use SMS to promote business sales?

Kalman filter -- Derivation from Gaussian fusion

DTD modeling

Leader: who can use redis expired monitoring to close orders and get out of here!

Swin-transformer --relative positional Bias
随机推荐
详解单例模式
Entropy - conditional entropy - joint entropy - mutual information - cross entropy
Construction and practice of full stack code test coverage and use case discovery system
MySQL download and installation tutorial
【社区明星评选】第23期 7月更文计划 | 点滴创作,汇聚成塔!华为FreeBuds 4E等酷爽好礼送不停
CTF flow analysis common questions (II) -usb flow
Lenovo Yoga 27 2022, full upgrade of super configuration
Evolution of screen display technology
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(上)
Small program container technology to promote the operation efficiency of the park
PC wechat multi open
The folder is transferred between servers. The folder content is empty
Build graphql service based on Actix, async graphql, rbatis, pgsql/mysql (4) - change service
The cloud native landing practice of using rainbow for Tuowei information
torch stack() meshgrid()
Ditto设置全局仅粘贴文本快捷键
German agbb VOC hazardous substances test
Cobbler is easy to use
JS string interception method summary
Delete duplicate elements in the sorting linked list ii[unified operation of linked list nodes --dummyhead]