C++中的移位问题

查看:112
本文介绍了C++中的移位问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在以下C/C++代码中,为什么结果还是41,根本没变
unsigned int d=41;
d=(d<<32)>>32;
cout<<" 结果"<<d;
所用电脑是win10 64位系统,VS2015和DEV-C++结果都是这样。
移位操作中还有哪些是值得我们注意的?

解决方案

c++标准约定当位移右侧操作数等于左侧操作数的位的个数时,运算会导致未定义行为。

5.8.1 The operands shall be of integral or unscoped enumeration type and integral promotions are performed. The type of the result is that of the promoted left operand. The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand.

这篇关于C++中的移位问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆