右移运算符
我今天花了几个小时寻找一个错误,直到我发现 我不明白的事情。
这是我一直在使用的代码:
unsigned long k,l,m;
k = 1000;
l = 33;
m = k>>l;
它给出了m=500
,即它显然移动了l mod 32
!
我逻辑上预期为 0。
这是我刚刚忽略并且从未注意到的事情吗?
I spent several hours today hunting for a bug until I found
something I don't understand.
This is the code I've been working with:
unsigned long k,l,m;
k = 1000;
l = 33;
m = k>>l;
It gives m=500
i.e. it apparently shifts by l mod 32
!
I have logically expected 0.
Is this something I have just overlooked and never noticed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C 中,我相信没有指定如果您使用的移位对于要移位的值的大小来说太大会发生什么,因此移位计数换行是完全可以接受的(这就是 x86 和 IIRC PowerPC 所做的) 。在 Java 中,您看到的包装行为是强制性的。
In C, I believe it is not specified what will happen if you use shifts that are too large for the size of the value that you are shifting, so having the shift count wrap is perfectly acceptable (and is what x86 and IIRC PowerPC do). In Java, the wrapping behavior you saw is mandatory.
ANSI C 标准规定:
因此,移位 33 与 33 次移位 1 不同......
The ANSI C standard says:
So, shifting by 33 isn't the same as 33 times shifting by one ...
您是否有可能混淆 1(一)和 l(字母 L),因为这以前发生在我身上! (也许可以通过更改 x=33;m = k>>x 来仔细检查?)
Is there a possibility you are confusing 1(one) and l(letter L) as this has happened to me before !! (double check perhaps by changing x=33; m = k>>x ?)