Subtracting a large unsigned binary number from a smaller one

最后都变了- 提交于 2019-12-05 18:14:49

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/9523210/subtracting-a-large-unsigned-binary-number-from-a-smaller-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!