简体   繁体   English

棘手的二元减法

[英]tricky binary subtraction

So I was practicing my binary subtraction. 所以我练习二元减法。 It's been a long while since my first exam and I decided to create my own tricky binary subtraction and I came up with this one: 从我的第一次考试开始已经有很长一段时间了,我决定创建自己棘手的二元减法,我想出了这个:

 1100
-1101

Of course the "borrowing trick" does not work for this problem at least I could not get it to work. 当然,“借用技巧”对这个问题不起作用,至少我无法让它发挥作用。 Is my only choice to flip the bits of the second binary number(the bottom one) and then add a one basically doing 2's complement so 1101 becomes 0011. Then add the primary binary number(1100) with the 2's complement representation(0011) which means it would look like this: 我唯一的选择是翻转第二个二进制数字的位(底部的一个),然后添加一个基本上做2的补码,这样1101就变成了0011.然后用2的补码表示法(0011)添加主二进制数(1100)。意味着它看起来像这样:

    1100 (-4) assume 2's complement
+   0011 (3)  assume 2's complement

sum:1111 (-1) assume 2's complement

I just need confirmation on this problem since its been a long time since I did binary subtraction. 我只需要确认这个问题,因为我做了二进制减法已经很长时间了。

 1100
-1101

0 - 1 = 1 (borrow 1) 0 - 1 = 1 (借1)

 1100
-1101
   1
=====
    1

0 - 0 - 1 = 1 (borrow 1) 0 - 0 - 1 = 1 (借1)

 1100
-1101
  11
=====
   11

1 - 1 - 1 = 1 (borrow 1) 1 - 1 - 1 = 1 (借1)

 1100
-1101
 111
=====
  111

1 - 1 - 1 = 1 (borrow 1) 1 - 1 - 1 = 1 (借1)

 1100
-1101
1111
=====
 1111

The result is 1111 with 1 borrowed. 结果是1111借来1。 In terms of unsigned arithmetic, this means that either the result underflowed or you need to borrow from the next significant digit. 就无符号算术而言,这意味着结果下溢或您需要从下一个有效数字借用。 (In terms of signed arithmetic there is no overflow as you have also borrowed the second bit and the calculation corresponds to -4 - -3 = -1 .) (就有符号算术而言,没有溢出,因为你还借了第二位,计算对应于-4 - -3 = -1 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM