加减二的补 [英] Adding and subtracting two's complement

查看:160
本文介绍了加减二的补的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用六位一个人的和补重presentation我试图解决以下问题:

Using six-bit one's and two's complement representation I am trying to solve the following problem:

12 - 7 

现在,我拿12二进制和7进位争先。

Now, i take 12 in binary and 7 in binary first.

12 = 001100 - 6 bit 
7 =  000111 - 6 bit

然后,我会然后翻转两个的补位和添加一个?

Then, would I then flip the bit for two's complement and add one?

12 = 110011 ones complement 
     +    1
    -------
     001101

7  = 111000 ones complement 
    +     1
   ---------
      111001

那么,加这两个互补在一起

then, add those two complement together

 001101
+111001
-------
1000110 = overflow? discard the last digit?  If so I get 5

现在,如果我有一个像

-15 + 2

我会再上MSB添加一个标志幅度,如果它是一个零?

I would then add a sign magnitude on the MSB if it's a zero?

这样的:

-15 = 001111 6 bit

我将在这里就结束了加1之前,我翻转位?

Would I add a 1 at the end here before I flip the bits?

  = 101111


推荐答案

使用二的补重新present负值具有减法和加法是相同的好处。你的情况,你能想到的 12 - 7 12 +(-7)。因此,你只需要找到补重-7 presentation并将其添加到+12:

Using two's complement to represent negative values has the benefit that subtraction and addition are the same. In your case, you can think of 12 - 7 as 12 + (-7). Hence you only need to find the two's complement representation of -7 and add it to +12:

12  001100
-7  111001   -- to get this, invert all bits of 7 (000111) and add 1
----------
 5 1000101

然后丢弃进(表示溢出),你有你的结果: 000101 如预期相等于5

有关你的的例子-15 + 2 ,只需按照相同的步骤,得到-15补重presentation:

For your example of -15 + 2, simply follow the same procedure to get the two's complement representation of -15:

15  001111
    110000   -- inverted bits
    110001   -- add 1

现在做加法和往常一样:

Now do the addition as usual:

-15  110001
  2  000010
-----------
res  110011

要看到 RES 确实等于-13,你可以看到它是负(MSB集)。对于大小,转换为正(反转位,加1):

To see that res indeed equals -13, you can see that it is negative (MSB set). For the magnitude, convert to positive (invert bits, add 1):

res  110011
     001100  -- inverted bits
     001101  -- add 1

因此​​,幅度为13预期。

Hence the magnitude is 13 as expected.

这篇关于加减二的补的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆