2的补码问题

发布于 2024-11-01 14:42:35 字数 148 浏览 3 评论 0原文

我有与 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技术交流群

发布评论

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

评论(2

你的呼吸 2024-11-08 14:42:35

如果我理解你的问题,那么你是对的。

如果它是一个 12 位二进制补码负数,则取出高位(通常称为位 11)并将其复制到其左侧的所有位中,以填充其 16 位的高位 4 位位等效。正如您在问题中所说,您拥有的 12 位值 - 0xfaa - 将成为 16 位值 0xffaa

在二进制中,12 位值是

    111110101010
    ^
    +---- bit 11 

并且将成为 16 位值。

 1111111110101010
 ^   ^
 |   +-------- bit 11 
 +------------ bit 15

两个字的长度不同并不会改变两个数字相等的事实。

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 value 0xffaa, just as you have said in your question.

In binary, the 12-bit value is

    111110101010
    ^
    +---- bit 11 

and becomes the 16-bit value

 1111111110101010
 ^   ^
 |   +-------- bit 11 
 +------------ bit 15

That the two words are of different lengths in no way changes the fact that the two numbers are equal.

凹づ凸ル 2024-11-08 14:42:35

您可以将最高有效数字设置为 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.

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