移64位整型 [英] Shift with 64 bit int
问题描述
我有一个__int64变量 X = 0x8000000000000000
。
I have a __int64 variable x = 0x8000000000000000
.
我试图向右字节来接班: X>> 4
I try to shift it right by byte : x >> 4
I`ve认为结果应该是 0x0800000000000000
,但不幸的是我得到 0xf800000000000000
。
I`ve thought that the result should be 0x0800000000000000
, but unfortunately I get 0xf800000000000000
.
我用VS10。为什么会这样呢?我怎么能解决呢?
I use VS10. Why is it so? And how can I solve that?
推荐答案
究其原因,是因为移符号数仅由语言定义的,如果左操作数至少是0。在你的情况下,我认为这是一个二进制补码重presentation和你的数字为负使得结果不确定的(或实现定义的,我没有手头上的参考现在)。通常情况下,你要么得到一个逻辑移位或算术移位。
The reason is because shifting signed numbers is only defined by the language if the left operand is at least 0. In your case I assume it's a twos-complement representation and your number is negative making the result unspecified (or implementation-defined, I don't have the reference at hand right now). Typically you would either get a logical shift or an arithmetic shift.
如果你能逃脱使你变的无符号,将解决你的问题。
If you can get away with making your variable unsigned that would solve your problem.
这篇关于移64位整型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!