当前位置:网站首页>小小笔记-整型提升(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
最后
以上位本篇笔记的全部内容,原码、反码和补码在上一篇文章讲过了故这次就讲的比较粗略。
最后感谢各位对小风扇的支持,一起努力啊!
边栏推荐
- MySQL transaction concurrency and mvcc mechanism
- Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
- Full recharge, im+rtc+x full communication service "feedback season" starts
- Word -- a solution for word to encounter errors when trying to open a file
- Nodejs 安装与介绍
- 基于UDP协议设计的大文件传输软件
- 「干货」数据分析常用的10种统计学方法,附上重点应用场景
- Construction and practice of full stack code test coverage and use case discovery system
- dtd建模
- TCP packet sticking problem
猜你喜欢

嵌入式软件开发新趋势:DevOps

熵-条件熵-联合熵-互信息-交叉熵

MySQL事务并发问题和MVCC机制

20220528【聊聊假芯片】贪便宜往往吃大亏,盘点下那些假的内存卡和固态硬盘

正则表达式(正则匹配)

浏览器窗口切换激活事件 visibilitychange

Swin-Transformer(2021-08)

Personally test the size of flutter after packaging APK, quite satisfied

Some interesting modules

Dlib library for face key point detection (openCV Implementation)
随机推荐
10 statistical methods commonly used for "dry goods" data analysis, with key application scenarios attached
3.10 haas506 2.0开发教程-example-TFT
Entry node of link in linked list - linked list topic
MySQL transaction concurrency and mvcc mechanism
Adhering to the concept of 'home in China', 2022 BMW children's traffic safety training camp was launched
MySQL download and installation tutorial
熵-条件熵-联合熵-互信息-交叉熵
20220607跌破建议零售价,GPU市场正全面走向供过于求...
Memory Limit Exceeded
MySQL modify data type_ MySQL modify field type [easy to understand]
Reading notes of "high EQ means being able to talk"
手机股票账号开户安全吗?是靠谱的吗?
sqlserver SQL Server Management Studio和Transact-SQL创建账户、创建访问指定数据库的只读用户
「杂谈」如何改善数据分析工作中的三大被动局面
Year after year, why is breaking the data island still the primary task of enterprise development
华兴证券:混合云原生架构下的 Kitex 实践
mysql函数获取全路径
服务器之间传文件夹,文件夹内容为空
Redis beginner to master 01
基于 actix、async-graphql、rbatis、pgsql/mysql 构建 GraphQL 服务(4)-变更服务