我有一个uint:
2147483648(仅打开第32位)
二进制是:
1000 0000 0000 0000 0000 0000 0000 0000
现在如果我将这个数字31位移到右边,结果应该是1,不应该吗?
但是我得到了这个值:4294967295
二进制:
1111 1111 1111 1111 1111 1111 1111 1111
我写了这个单元测试:
[Test]
public function testShift(): void {
// shift 31st bit 30 bits to the right -> WORKS!!!
var bit31:uint = 1073741824; // 0100 0000 0000 0000 0000 0000 0000 0000
var test1:uint = bit31 >> 30;
assertEquals(1, test1);
// shift 32nd bit 31 bits to the right -> FAILS!!!
var bit32:uint = 2147483648; // 1000 0000 0000 0000 0000 0000 0000 0000
var test2:uint = bit32 >> 31;
assertEquals(1, test2);
}
Run Code Online (Sandbox Code Playgroud)
有人可以赐教吗?
谢谢!
**编辑**
这是单元测试输出:
预期:<1>但是:<4294967295>
**编辑2**
有一段时间我以为我失去了理智.我用c#尝试了同样的方法.这是一个在线交互式shell 的链接.
UInt32 token = 2147483648;
UInt32 result = token >> 31;
Console.Write(result);
Run Code Online (Sandbox Code Playgroud)
使用c#,结果是1.正如所料......
| 归档时间: |
|
| 查看次数: |
115 次 |
| 最近记录: |