为什么我会得到负值位移的奇怪结果?

发布于 2024-08-14 09:32:22 字数 1157 浏览 3 评论 0原文

此问题与此问题不重复。

我遇到过一种情况,我可能必须将(正)数字左移负值,即 8 << -1。在这种情况下,我预计结果是 4,但我以前从未这样做过。因此,我编写了一个小测试程序来验证我的假设:

for (int i = -8; i <= 4; i++)
    Console.WriteLine("i = {0}, 8 << {0} = {1}", i, 8 << i);

令我震惊和惊讶的是,它给出了以下输出:

i = -8, 8 << -8 = 134217728
i = -7, 8 << -7 = 268435456
i = -6, 8 << -6 = 536870912
i = -5, 8 << -5 = 1073741824
i = -4, 8 << -4 = -2147483648
i = -3, 8 << -3 = 0
i = -2, 8 << -2 = 0
i = -1, 8 << -1 = 0
i = 0, 8 << 0 = 8
i = 1, 8 << 1 = 16
i = 2, 8 << 2 = 32
i = 3, 8 << 3 = 64
i = 4, 8 << 4 = 128

任何人都可以解释这种行为吗?

这是一个小奖励。我将左移更改为右移,并得到以下输出:

i = -8, 8 >> -8 = 0
i = -7, 8 >> -7 = 0
i = -6, 8 >> -6 = 0
i = -5, 8 >> -5 = 0
i = -4, 8 >> -4 = 0
i = -3, 8 >> -3 = 0
i = -2, 8 >> -2 = 0
i = -1, 8 >> -1 = 0
i = 0, 8 >> 0 = 8
i = 1, 8 >> 1 = 4
i = 2, 8 >> 2 = 2
i = 3, 8 >> 3 = 1
i = 4, 8 >> 4 = 0

This question is NOT a duplicate of this question.

I came across a situation where I might have had to left-shift a (positive) number by a negative value, i.e., 8 << -1. In that case, I would expect the result to be 4, but I'd never done this before. So I made up a little test program to verify my hypothesis:

for (int i = -8; i <= 4; i++)
    Console.WriteLine("i = {0}, 8 << {0} = {1}", i, 8 << i);

which to my shock and surprise gave me the following output:

i = -8, 8 << -8 = 134217728
i = -7, 8 << -7 = 268435456
i = -6, 8 << -6 = 536870912
i = -5, 8 << -5 = 1073741824
i = -4, 8 << -4 = -2147483648
i = -3, 8 << -3 = 0
i = -2, 8 << -2 = 0
i = -1, 8 << -1 = 0
i = 0, 8 << 0 = 8
i = 1, 8 << 1 = 16
i = 2, 8 << 2 = 32
i = 3, 8 << 3 = 64
i = 4, 8 << 4 = 128

Can anyone explain this behaviour?

Here's a little bonus. I changed the left-shift to a right-shift, and got this output:

i = -8, 8 >> -8 = 0
i = -7, 8 >> -7 = 0
i = -6, 8 >> -6 = 0
i = -5, 8 >> -5 = 0
i = -4, 8 >> -4 = 0
i = -3, 8 >> -3 = 0
i = -2, 8 >> -2 = 0
i = -1, 8 >> -1 = 0
i = 0, 8 >> 0 = 8
i = 1, 8 >> 1 = 4
i = 2, 8 >> 2 = 2
i = 3, 8 >> 3 = 1
i = 4, 8 >> 4 = 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愚人国度 2024-08-21 09:32:23

您不能移动负值。您也不能移动很大的正数。

来自 C# 规范 (http://msdn.microsoft.com/en-us/库/a1sway8w.aspx):

If first operand is an int or uint (32-bit quantity), 
the shift count is given by the low-order five bits of second operand.

...


The high-order bits of first operand are discarded and the low-order 
empty bits are zero-filled. Shift operations never cause overflows.

You can't shift by a negative value. You also can't shift by a large positive number.

From the C# spec (http://msdn.microsoft.com/en-us/library/a1sway8w.aspx):

If first operand is an int or uint (32-bit quantity), 
the shift count is given by the low-order five bits of second operand.

...


The high-order bits of first operand are discarded and the low-order 
empty bits are zero-filled. Shift operations never cause overflows.
三生一梦 2024-08-21 09:32:23

在类 C 语言中 << -1 不会转换为 >> 1..相反,将采用移位的最低有效 5 位,而忽略其余位,因此在这种情况下,二进制补码 -1 会转换为 << 31。

你会得到相同的结果,例如。 JavaScript javascript:alert(8<<-8)

In C-like languages << -1 doesn't translate to >> 1. Instead the least-significant 5 bits of the shift are taken and the rest ignored, so in this case the two's complement -1 translates to << 31.

You'll get the same results from eg. JavaScript javascript:alert(8<<-8).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文