C99标准学习笔记(2)——整数提升
2015-08-08 17:27
295 查看
Usual Arithmetic Conversion: The integer promotions are performed
on both operands. Then the following rules are applied to the promoted operands:
If both operands have the same type, then no further conversion is needed.
Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with
the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of
the other operand, then the operand with signed integer type is converted the type of the operand with unsigned integer type.
Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of
the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand
with signed integer type.
献丑翻译成中文:
整数提升:整数提升可以应用于所有的操作数,下面是整数提升的规则:
如果两个操作数具有相同的类型,那么不需要任何的转换;
不然,如果两个操作数同为有符号整数或者同为无符号整数,则rank小的操作数需要被提升为rank大的操作数的类型;
不然,一个无符号操作数的rank大于或者等于另外一个有符号操作数,则该有符号操作数的类型会提升为无符号操作数的类型;
不然,一个有符号操作数可以表示另一个无符号数的所有的值,那么那个无符号操作数的类型需转换为该有符号类型;
不然,两个操作数都需要转换为有符号数对应的无符号类型——这种情况,我没有找出实例,哪位朋友指点一下呢。
虽然整数类型提升为C的基础概念,但是不看标准的话,我想大多数人不会特别清楚——包括我在内,即使平时因为注意这个问题,而不会因此犯错。尤其是第4条,至少我以前就不甚了解。我会认为有符号数都会转为无符号数的,不会认为无符号数也会转为有符号数的。
为此,我又写了一篇博文http://blog.chinaunix.net/space.php?uid=23629988&do=blog&id=2938698专门针对具体的整数提升情况
更新:
此文还有理解不深的地方,另也一篇博文更新:http://blog.chinaunix.net/space.php?uid=23629988&do=blog&id=2950299
on both operands. Then the following rules are applied to the promoted operands:
If both operands have the same type, then no further conversion is needed.
Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with
the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of
the other operand, then the operand with signed integer type is converted the type of the operand with unsigned integer type.
Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of
the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand
with signed integer type.
献丑翻译成中文:
整数提升:整数提升可以应用于所有的操作数,下面是整数提升的规则:
如果两个操作数具有相同的类型,那么不需要任何的转换;
不然,如果两个操作数同为有符号整数或者同为无符号整数,则rank小的操作数需要被提升为rank大的操作数的类型;
不然,一个无符号操作数的rank大于或者等于另外一个有符号操作数,则该有符号操作数的类型会提升为无符号操作数的类型;
不然,一个有符号操作数可以表示另一个无符号数的所有的值,那么那个无符号操作数的类型需转换为该有符号类型;
不然,两个操作数都需要转换为有符号数对应的无符号类型——这种情况,我没有找出实例,哪位朋友指点一下呢。
虽然整数类型提升为C的基础概念,但是不看标准的话,我想大多数人不会特别清楚——包括我在内,即使平时因为注意这个问题,而不会因此犯错。尤其是第4条,至少我以前就不甚了解。我会认为有符号数都会转为无符号数的,不会认为无符号数也会转为有符号数的。
为此,我又写了一篇博文http://blog.chinaunix.net/space.php?uid=23629988&do=blog&id=2938698专门针对具体的整数提升情况
更新:
此文还有理解不深的地方,另也一篇博文更新:http://blog.chinaunix.net/space.php?uid=23629988&do=blog&id=2950299
相关文章推荐
- Logistic回归
- ListView性能优化问题总结
- 正则表达式代码生成
- 获取屏幕宽高和密度
- 【原创】文本工具的使用--根据数据库字段快速生成该表对应的Model类属性
- 左右相等问题
- hdu 5363 Key Set 快速幂
- iOS开发网络篇—NSURLConnection基本使用(一)
- 代码ARC+1:—1:0
- C++_运算符重载
- hdu 5215 Cycle
- Unity5.0 EventSystem事件系统的详细说明
- hpuoj 1692 找女友
- 表达式求值
- C++运算符重载(二)——使用方法
- Ignatius and the Princess IV 水
- 堆排序HeapSort
- C++(18)再谈指针
- 几种编码方式
- iOS开发网络篇—数据安全