博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
integer promotion
阅读量:4459 次
发布时间:2019-06-08

本文共 1515 字,大约阅读时间需要 5 分钟。

用小米的笔试题来举例吧

1 void fun()   2 {   3     unsigned int a = 2013;   4     int b = -2;   5     int c = 0;   6     while (a + b > 0)   7     {   8         a = a + b;   9         c++;  10     }  11     printf("%d", c);  12 }  问输出什么? 错误答案是1003,参考解释如下 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 to the type of the operand with unsigned integer type.(当一个为符号型,另一个为无符号型且无符号型的范围更广时,都调整为无符号型,题目中a能表示到OXFFFFFFFF,而b最大到7FFFFFFF) 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. 此题明显调整为无符号型,而unsigned int的表示范围是0~4294967295,所以就看看a+b有不有可能为0就行了,很显然,a=2013,每次减2,当减到为1时,unisigned int(1-2)就不是-1了,而是4294967295(因为负数是按补码表示的,-1的补码为全1),所以这个函数一直在while循环中执行,无法跳出

转载于:https://www.cnblogs.com/encode/archive/2013/03/17/2964918.html

你可能感兴趣的文章
对Netflix Ribbon的Loadbalancer类源码设计合理性的一点质疑
查看>>
关于日历的算法
查看>>
[QT编程]QT实现的一个渐隐渐显窗体
查看>>
在Web工程中引入Jquery插件报错解决方案
查看>>
大学总结之影响我最深的十本书
查看>>
用myEclipse连接数据源生成动态数据报表
查看>>
[myeclipse]@override报错问题
查看>>
자주 쓰이는 정규표현식
查看>>
超简单的listview单选模式SingleMode(自定义listview item)
查看>>
vue-11-路由嵌套-参数传递-路由高亮
查看>>
HDU 1199 - Color the Ball 离散化
查看>>
[SCOI2005]骑士精神
查看>>
Hibernate原理解析-Hibernate中实体的状态
查看>>
六时车主 App 隐私政策
查看>>
C语言常见问题 如何用Visual Studio编写C语言程序测试
查看>>
Web用户的身份验证及WebApi权限验证流程的设计和实现
查看>>
hdu 2098 分拆素数和
查看>>
ECMAScript6-let与const命令详解
查看>>
iOS 使用系统相机、相册显示中文
查看>>
什么是敏捷设计
查看>>