【问题标题】:Finding 2's complement of negative no找到负数的 2 的补码
【发布时间】:2013-05-10 20:29:07
【问题描述】:

我们假设使用 4bit 2 的补码系统来表示正数和负数

Suppose  we have to find 2's complement of no.  3
We can subtract this no. with 2^4
              that is ,                   
                           = 2^4 - 3
                           = 13  
                           =1101 (which represent -3 , in 2's complement system)

//* there is another way of finding 2's complement , taking 1's complemen of the 
    number and add 1 to it.

在书中给出的规则是减去两个没有。

规则: 减去两个 no X 和 Y ,即 X - Y ,形成 2 的 no 补码。 Y 并将其添加到 X。

假设我们必须减去两个不。 (-7)和(-5),那么根据规则,我们需要找到第(-5)的2的补码,然后将其添加到(-7)中。

预订解决方案:

我需要知道,-5 的 2 的补码是 0101。

【问题讨论】:

    标签: math programming-languages numbers computer-science


    【解决方案1】:

    要找到一个数字的 2 补码:

    • 反转位
    • 加1

    示例(5 的 2s 补码):

    • 5 = 0101 二进制

    • 反转位:

      1010

    • 加1:

      1011 1011

    示例(-5 的 2s 补码):

    • -5 = 1011 二进制

    • 反转位:

      0100

    • 加1:

      0101 0101

    【讨论】:

    • 但 no 不是 5 ,它是 -5 并且在 2 的补码系统中,-5 的位表示是 1011。
    • 如果你对一个负数执行同样的 2s 补码运算,你会得到正数。
    • 我们如何通过从 2^4 中减去它来找到相同的值?
    • 2^4 - (the number) = (2s complement) 用于 4 位,并且您只使用最低有效的 4 位。所以2^4 - (-5) = 21,即0b10101,但由于我们只想要最低有效的4位,所以2s补码将是0b0101,即5。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    相关资源
    最近更新 更多