惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
O
OpenAI News
D
Docker
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
K
Kaspersky official blog
Security Latest
Security Latest
T
Tailwind CSS Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
N
News and Events Feed by Topic
aimingoo的专栏
aimingoo的专栏
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Help Net Security
Help Net Security
爱范儿
爱范儿
宝玉的分享
宝玉的分享
腾讯CDC
H
Heimdal Security Blog
Webroot Blog
Webroot Blog
AI
AI
WordPress大学
WordPress大学
Recorded Future
Recorded Future
SecWiki News
SecWiki News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
C
Check Point Blog
TaoSecurity Blog
TaoSecurity Blog
Cisco Talos Blog
Cisco Talos Blog
The Cloudflare Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - Franky
云风的 BLOG
云风的 BLOG

博客园 - Lunais

恢复 git stash drop 丢失的内容 Git技巧:彻底重置本地仓库与远程同步,同时保留Stash内容 信源编码和信道编码区别 Leetcode scanf 一组数据差值之和最大 5G 3gpp协议 eclipse调试配置 find查找 浮点类型(float、double)在内存中的存储 yield [基础函数]memset用法 python2处理表格文件按照规则多行输出(中文路径,print) linux系统objdump输出动态库.so文件和静态库.a中符号表 python复制删除文件&修改目录&执行bat(wins) Linux C下的正则表达式 kill eclipse 查找函数对比:findall,search,match Linux backtrace() git本地协同
C语言之表达式运算整体提升
Lunais · 2021-01-05 · via 博客园 - Lunais

整型提升是C语言的一种规则,由C语言的发明人丹尼斯·里奇与肯·汤普逊创设:

"A character, a short integer, or an integer bit-field, all either signed or not, or an object of enumeration type, may be used in an expression wherever an integer maybe used. If an int can represent all the values of the original type, then the value is converted to int; otherwise the value is converted to unsigned int. This process is called integral promotion."

表达式中可以使用整数的地方,就可以使用枚举类型,或有符号或无符号的字符、短整数、整数位域。如果一个int可以表示上述类型,则该值被转化为int类型的值;否则,该值被转化为unsigned int类型的值。这一过程被称作integral promotion。

整型提升的意义在于:

表达式的整型运算要在CPU的相应运算器件内执行,CPU内整型运算器(ALU)的操作数的字节长度一般就是int的字节长度,同时也是CPU的通用寄存器的长度。因此,即使两个char类型的相加,在CPU执行时实际上也要先转换为CPU内整型操作数的标准长度。通用CPU(general-purpose CPU)是难以直接实现两个8比特字节直接相加运算(虽然机器指令中可能有这种字节相加指令)。所以,表达式中各种长度可能小于int长度的整型值,都必须先转换为int或unsigned int,然后才能送入CPU去执行运算。

 同样的,还有浮点类型提升:

当表达式中最长的是long double类型,则其他类型提升为long double类型再进行运算;当最长类型为double则其他类型提升至double类型;当最长类型为float则其他类型提升至float类型。整型全部转换为对应浮点类型。

对于一个表达式,根据K&R中描述的具体转换规则如下:

① 首先,如果参与运算的任何一个操作数为 long double 类型,则将另一个操作数转换为 long double 类型。

② 否则,如果任何一个操作数为 double 类型,则将另一个操作数转换为 double 类型。

③ 否则,如果任何一个操作数为 float 类型,则将另一个操作数转换为 float 类型。

④ 否则,同时对两个操作数进行整型提升。

⑤ 然后,如果任何一个操作数为 unsigned long int 类型,则将另一个操作数转换为 unsigned long int 类型。

⑥ 否则,如果一个操作数为 long int 另一个为 unsigned int 类型,则结果依赖于 long int 类型能否表示所有的 unsigned int 类型;如果可以则将 unsigned int 类型转换 long int (64位系统下 long 是8byte,可以表示 unsigned int ,但是32位环境不可以表示)。如果不可以,则将两个数都转为 unsigned long int 类型。

⑦ 否则,如果任何一个操作数为 long int 类型,则将另一个操作数转换为 long int 类型。

⑧ 否则,如果任何一个操作数为 unsigned int 类型,则将另一个操作数转换为 unsigned int 类型。

⑨ 否则,将两个操作数都转换为 int 类型。