1

Based on the method in this question Using 2's Complement to Perform Binary Division for Signed Number , I tried to calcuate 12 / 10, which is 0000 1100 / 0000 1010 , but with its method I got quotient 2 and remainder -8.

Did I get something wrong?

step 1 omitted
 
step 2:

 0000 1100
+1111 0110
----------
10000 0010 

carry bit discarded, quotient=0+1=1 , do step 3

step 3:

 0000 0010
+1111 0110
----------
 1111 1000

calculation stopped, quotient=1+1=2 , remainder is 1111 1000 = -8
Rick
  • 7,007
  • 2
  • 49
  • 79
  • I think you just went one step too far? `12 = 10*0 + 12 = 10*1 + 2 = 10*2 + (-8)`, so the (q,r) pairs `(0, 12), (1, 2), (2, -8)` all correctly satisfy `12 = 10q + r`, however there is only one pair which also satisfies `0 <= r < 10`, and you should stop the algorithm exactly when that happens. If you continue for one more step you'll end up with a negative `r`. – Stef Jun 27 '23 at 08:58
  • @Stef But according to the method in that question, I have to do the last step, resulting in a negative remainder, and that's what got me confused. I wanted to stop when the quotient is `1` and remainder is `2`. I checked the method and I think I did follow the rules. – Rick Jun 27 '23 at 09:14
  • I confess that I have not studied their particular method in detail, so I cannot be sure what the issue is. Perhaps they're going one step too far on purpose, because they think that "stop when `r < 0`" is a simpler instruction than "stop when `r < divisor`". But if you do that, you need to remove 1 from `q` and add `divisor` to `r`. – Stef Jun 27 '23 at 12:25
  • Another possibility is that their definition of "quotient and remainder" is somewhat nonstandard. Mathematicians mostly all use the same definition for quotient and remainder, but computer people have never agreed on a definition. For instance if you test python's `//` and `%` operators, and then you test C's `/` and `%` operators, you will get different results, especially if any of the dividend and divisor are negative. – Stef Jun 27 '23 at 12:28

0 Answers0