本文介绍了移位的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无法理解这种移位的行为:
Can't understand behavior of this bit shift:
int container = 1;
cout<<(container>>32)<<endl;
如果是逻辑移位,则输出应为0,但应为1,就像循环移位一样.在查看反汇编时,我看到所使用的命令是SAR.请向我解释这种行为.
If it's logical shift the output should be 0, but it's 1 instead, as if it was cyclic shift. When looking at disassembly I see that command used is SAR. Please explain this behavior to me.
推荐答案
您将32位数字移位了32,这导致行为未定义,结果1是一个巧合.
You shifted a 32-bit number by 32, which results in undefined behavior, and the resulting 1 is a coincidence.
这篇关于移位的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!