Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtracting a large unsigned binary number from a smaller one

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.

like image 680
Anthony Jack Avatar asked Mar 01 '12 20:03

Anthony Jack


People also ask

How can the subtraction be carried to subtract a greater number from a smaller number?

Subtracting a larger number from a smaller number will result in a negative answer - since we are taking away more than we started with. One way to remember this is to think of money: if you try to spend more than you have, you end up with a negative amount of money (meaning you owe more money).

What are the rules to subtract two binary numbers?

Binary Subtraction. Binary subtraction is also similar to that of decimal subtraction with the difference that when 1 is subtracted from 0, it is necessary to borrow 1 from the next higher order bit and that bit is reduced by 1 (or 1 is added to the next bit of subtrahend) and the remainder is 1.


1 Answers

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.

like image 198
Alexey Frunze Avatar answered Oct 05 '22 04:10

Alexey Frunze