2的补码问题
我有与 2 的补码相关的 aq。 假设我有一个 2 的补码表示形式的有符号 16 位十六进制 例如,
如果它是16位,则取0xfaa,我需要扩展它,因为它现在只有12位。我知道我必须扩展左侧,所以它会变成0xffaa。
这意味着我的数字是负数,对吗?
I have a q related to 2's complement.
Say I have a signed 16 bit hexadecimal in 2's complement representation
for example, take 0xfaa
if it's 16 bit, i need to expand it cause it only has 12 bits now. I know i have to expand the left side, so it'd be made into 0xffaa.
that means that my number is negative, is that correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解你的问题,那么你是对的。
如果它是一个 12 位二进制补码负数,则取出高位(通常称为位 11)并将其复制到其左侧的所有位中,以填充其 16 位的高位 4 位位等效。正如您在问题中所说,您拥有的 12 位值 -
0xfaa
- 将成为 16 位值0xffaa
。在二进制中,12 位值是
并且将成为 16 位值。
两个字的长度不同并不会改变两个数字相等的事实。
If I understand your question, then you are correct.
If it's a 12-bit two's complement negative number, then you take the high-order bit -- bit 11, as it's usually called -- and copy it into all bits to its left to fill the high-order 4 bits of its 16-bit equivalent. The 12-bit value you have --
0xfaa
-- becomes the 16-bit value0xffaa
, just as you have said in your question.In binary, the 12-bit value is
and becomes the 16-bit value
That the two words are of different lengths in no way changes the fact that the two numbers are equal.
您可以将最高有效数字设置为 0,因此它将是 0x0faa。如果你假设 f 到达那里,你就改变了数字的值。
You would make the most significant digit a 0, so it would be 0x0faa. If you assume and f goes there you have changed the value of the number.