有符号整数的逻辑右移运算

发布于 2024-09-26 11:50:52 字数 167 浏览 4 评论 0原文

对有符号整数 -28 进行逻辑右移 3 操作。正确答案是什么?

  1. +203
  2. +83
  3. +3
  4. -3

-28 的 2 的补码是 11100100。 现在,如果我应用逻辑右移运算,我不会得到上述任何一个答案。

Logical shift right by 3 operation on signed integer -28. What's the correct answer?

  1. +203
  2. +83
  3. +3
  4. -3

2's complement of -28 is 11100100.
Now if I apply logical right shift operation I am not getting any one of above answers.

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

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

发布评论

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

评论(4

感情旳空白 2024-10-03 11:50:53

我几乎可以肯定这是一个棘手的问题。

面试官正在看你是否会回答“-3”。如果你愿意(错误地推理,因为 28>>3 是 3,那么 -28>>3 是 -3),他会意识到你不理解二进制补码。

他要你回答这四个选项都不正确。他想让你

  1. 解释逻辑如何右移,
    与算术右移不同,
    把一个小的负数变成一个巨大的数
    通过转动符号得到正数
    深入研究部分幅度,
  2. 指出答案取决于
    用多少个字节来表示
    一个整数

I'm almost certain it's a trick question.

The interviewer was seeing if you would answer "-3". If you would have (with the faulty reasoning that since 28>>>3 is 3 then -28>>>3 is -3), he would have realized that you don't understand two's complement.

He wanted you to answer that none of the four choices is correct. He wanted you to

  1. explain how logical shift right,
    unlike arithmetic shift right, would
    turn a small neg number into a huge
    positive number by turning the sign
    bit into part of the magnitude
  2. point out that the answer depends on
    how many bytes are used to represent
    an int
睡美人的小仙女 2024-10-03 11:50:53

也许诀窍不在于假设二进制补码表示形式。假设sign-and-magnitude表示,答案可以是-3,因为大多数移位实现不涉及符号位。

Maybe the trick is not to assume two's complement representation. Assuming sign-and-magnitude representation, the answer can be -3, since most shifting implementations would not involve the sign bit.

完美的未来在梦里 2024-10-03 11:50:53

这是一个愚蠢的问题,因为:

  • 负数在左边添加 0 还是 1 并没有被普遍定义(维基百科说“空位被填充,通常用零填充” - 我的强调)
  • 未讨论所涉及的整数大小,并且
  • 使用了负数的多种按位表示形式。

某些语言(如我认为的 Java)使用了新的最高有效位,因此任何没有合适 CPU 指令的平台都必须发出多个指令来计算所需的答案,而其他语言可能会在CPU 本身提供的行为。

2 的补码是负数最常见的表示形式。你的问题指出“-28的2的补码是11100100。”...我猜这没有作为问题的一部分提供(如果是的话有点奇怪,因为它在答案之后)。仍然...

如果我们用 2 的补码运行...

11100100 >> 3 = 00011100 or 11111100 = 28 or -4

如果表示是 1 的补码:

11100011 >> 3 = 00011100 or 11111100 = 28 or -3

如果表示是符号位,绝对值:

10011100 >> 3 = 00010011 or 11110011 = 19 or -(127-12)=-115

(note that the question says a logical bit shift, which by definition ignores any possible interpretation of the bits, so the sign bit is shifted along with others)

Re #bits in an int...我认为这很明显,那就是导致值太大而无法匹配任何选项,否则不会有任何区别(如果在左侧添加 1 作为 1 或 2 的补码),我们可以忽略该问题。

因此,除非我上面的快速计算出错,否则 -3 是在任何合理的架构上唯一可能正确的答案,但可能性仍然很小。总而言之,我想知道他们是否实际上没有进行测试,看看谁有信心不回答问题,或者注释说没有一个答案可能是正确的......

It's a stupid question as:

  • whether a 0 or a 1 is added at the left isn't universally defined for negative numbers (Wikipedia says "vacant bit-positions are filled in, generally with zeros" - my emphasis)
  • the integer size involved isn't discussed, and
  • there are multiple bitwise representations of negative numbers in use.

Some languages (like Java I believe) the new most-significant bits such that any platform that doesn't have a suitable CPU instruction will have to issue several to calculate the required answer, whereas other languages may make an implementation-defined choise between the behaviours the CPU natively offers.

2's complement is the most common representation of negative numbers. Your question states "2's complement of -28 is 11100100."... I'm guessing that wasn't provided as part of the question (a bit weird if so, as it's after the answers). Still...

If we run with 2's complement...

11100100 >> 3 = 00011100 or 11111100 = 28 or -4

If the representation was 1's complement:

11100011 >> 3 = 00011100 or 11111100 = 28 or -3

If the representation was sign-bit, absolute-value:

10011100 >> 3 = 00010011 or 11110011 = 19 or -(127-12)=-115

(note that the question says a logical bit shift, which by definition ignores any possible interpretation of the bits, so the sign bit is shifted along with others)

Re #bits in an int... I think it's so obvious that that would either result in a value that's too large to match any of the options, or else it wouldn't make any difference (if 1s are being added at left for a 1's or 2's complement), that we can ignore that issue.

So, unless my quick calculation above slipped up, -3 is the only answer that could be correct on any plausible architecture, but is still very unlikely. All up, I'm wondering if they weren't actually testing to see who had the confidence to leave the question unanswered, or annotate that none of the answers were likely correct....

反话 2024-10-03 11:50:53

将有符号整数右移可以做很多事情:

  1. 如果数字为负数,它将对结果进行符号扩展(在左侧移动 1 位),这应该具有使数字看起来像较小的负数。
  2. 如果数字是正数,则每移动一位,它就会将其除以二。

但这种行为在技术上是“实现定义的”。

请参阅这篇文章: 移位运算符(<<、>>)是算术运算符还是逻辑运算符C?

Shifting a signed integer to the right can do a number of things:

  1. if the number was negative, it'll sign extend the result (shifts in 1 bits on the left side), which should have the effect of making the number look like a smaller negative number.
  2. If the number was positive, it'll divide it by two for each bit you shifted.

But this behavior is technically "implementation defined."

Please see this post: Are the shift operators (<<, >>) arithmetic or logical in C?

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