Strange behavior of bit-shift [duplicate]

£可爱£侵袭症+ 提交于 2019-12-11 14:50:53

问题


Can't understand behavior of this bit shift:

int container = 1;

cout<<(container>>32)<<endl;

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.


回答1:


You shifted a 32-bit number by 32, which results in undefined behavior, and the resulting 1 is a coincidence.



来源:https://stackoverflow.com/questions/31794709/strange-behavior-of-bit-shift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!