有符号整数的逻辑右移运算
对有符号整数 -28 进行逻辑右移 3 操作。正确答案是什么?
- +203
- +83
- +3
- -3
-28 的 2 的补码是 11100100。 现在,如果我应用逻辑右移运算,我不会得到上述任何一个答案。
Logical shift right by 3 operation on signed integer -28. What's the correct answer?
- +203
- +83
- +3
- -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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我几乎可以肯定这是一个棘手的问题。
面试官正在看你是否会回答“-3”。如果你愿意(错误地推理,因为 28>>3 是 3,那么 -28>>3 是 -3),他会意识到你不理解二进制补码。
他要你回答这四个选项都不正确。他想让你
与算术右移不同,
把一个小的负数变成一个巨大的数
通过转动符号得到正数
深入研究部分幅度,
用多少个字节来表示
一个整数
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
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
how many bytes are used to represent
an int
也许诀窍不在于假设二进制补码表示形式。假设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.
这是一个愚蠢的问题,因为:
某些语言(如我认为的 Java)使用了新的最高有效位,因此任何没有合适 CPU 指令的平台都必须发出多个指令来计算所需的答案,而其他语言可能会在CPU 本身提供的行为。
2 的补码是负数最常见的表示形式。你的问题指出“-28的2的补码是11100100。”...我猜这没有作为问题的一部分提供(如果是的话有点奇怪,因为它在答案之后)。仍然...
如果我们用 2 的补码运行...
如果表示是 1 的补码:
如果表示是符号位,绝对值:
Re #bits in an int...我认为这很明显,那就是导致值太大而无法匹配任何选项,否则不会有任何区别(如果在左侧添加 1 作为 1 或 2 的补码),我们可以忽略该问题。
因此,除非我上面的快速计算出错,否则 -3 是在任何合理的架构上唯一可能正确的答案,但可能性仍然很小。总而言之,我想知道他们是否实际上没有进行测试,看看谁有信心不回答问题,或者注释说没有一个答案可能是正确的......
It's a stupid question as:
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...
If the representation was 1's complement:
If the representation was sign-bit, absolute-value:
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....
将有符号整数右移可以做很多事情:
但这种行为在技术上是“实现定义的”。
请参阅这篇文章: 移位运算符(<<、>>)是算术运算符还是逻辑运算符C?
Shifting a signed integer to the right can do a number of things:
But this behavior is technically "implementation defined."
Please see this post: Are the shift operators (<<, >>) arithmetic or logical in C?