12

I'm taking a computer organization and assembly language course. The written part of our lab this week has a question on it that has me stumped. The question reads...

Subtract the following unsigned binary numbers (show the borrow and overflow bits). Do not convert to two's complement.

 0101 0111 1101
-1110 1011 0110
 --------------

I realize that the answer is -1001 0011 1001 but I'm having a hard time trying to figure out how to borrow to actually perform this subtraction by taking the larger number and subtracting it from the smaller number and show my work. My whole life when subtracting a large number from a small number I have reversed the problem and instead subtracted the smaller number from the larger number and added a negative sign in front of the result. I asked the professor and he says that he wants the problem solved the way that it is written. I am not allowed to solve this by subtracting the smaller number from the larger number and negating like I normally would. I haven't been able to find any examples online of subtracting a larger unsigned binary number from a smaller one.

I would really appreciate it if someone could describe to me how to perform subtraction in this scenario.

Update: @Alex is correct. The professor was looking for

0110 1100 0111 (1735)

Thanks everyone.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Anthony Jack
  • 5,333
  • 7
  • 28
  • 47
  • 2
    Are you sure he doesn't want `0110 1100 0111` with an indication of some borrow bits and an overflow bit? – Beta Mar 01 '12 at 22:42
  • @Beta: No, the answer is `-1001 0011 1001` (-2361). He's just working with normal binary numbers, not a particular computer representation like 2's-complement. – BlueRaja - Danny Pflughoeft Mar 02 '12 at 17:36
  • I'm not sure if the professor was after -1001 0011 1001(-2361) or 0110 1100 0111(1735), I ended up going with 1735. I think that @Beta is right. I'll post back with what he was looking for when the lab is handed back. – Anthony Jack Mar 02 '12 at 17:51
  • Hooking http://math.stackexchange.com/q/1270754/13733 – Pacerier Apr 07 '17 at 20:15

2 Answers2

9

You do it the same way irrespective of which number is bigger and which is smaller.

 bb b      bb   <- borrows
 0101 0111 1101 (1405)
-1110 1011 0110 (3766)
 --------------
 0110 1100 0111 (1735?)

Now, if you want a proper difference, you need to take into account the overflow since the above result doesn't include the sign bit:

 b bb b      bb   <- borrows
 0 0101 0111 1101 (1405)
-0 1110 1011 0110 (3766)
 ----------------
 1 0110 1100 0111 (-2361 signed 2's complement)

Really, the CPU doesn't care what gets subtracted from what. It uses the same algorithm for integer addition/subtraction, moreover, this algorithm is the same for signed and unsigned integers. You only have to correctly interpret the result and the carry and overflow flags. That's all.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • Thanks Alex. I'm suspect that your top example is what the professor is after. That's what I kept getting when trying to solve the problem. I think that may have been the point of the problem... to show that the subtraction doesn't yield the right result in this scenario. I think I was just approaching the problem from the wrong mindset. – Anthony Jack Mar 02 '12 at 17:46
  • @BlueRaja-DannyPflughoeft: He said "Do not convert to two's complement." and I didn't convert anything. – Alexey Frunze Mar 02 '12 at 18:16
  • How is the MSD 0 - 1 - 1 = 0? – Abhijit Sarkar Mar 17 '19 at 21:42
  • @AbhijitSarkar What else should be there if you have only two binary digits (0 and 1) and need to add/subtract 1 two times? – Alexey Frunze Mar 17 '19 at 22:28
  • @AlexeyFrunze could just as well be 1. I’m trying to understand the logic behind why it’s 0. – Abhijit Sarkar Mar 17 '19 at 22:50
  • 1
    @AbhijitSarkar In modulo-2 arithmetic adding/subtracting 1 inverts the digit (0 becomes 1 or 1 becomes 0). Doing that twice preserves it. There just isn't any other possibility. – Alexey Frunze Mar 18 '19 at 06:16
  • @AlexeyFrunze Thanks, so it's basically a XOR operation. – Abhijit Sarkar Mar 18 '19 at 07:16
  • @AbhijitSarkar Correct, the output digit is a XOR of three things: the two input digits and the carry-in/borrow-in. – Alexey Frunze Mar 18 '19 at 07:20
-1

simply subtract the two binary numbers as they are, then take the 2's complement of the result. voila!