# Binary Division with Signed Numbers

I am taking an electrical engineering course as part of my computer science degree and we are talking about how a computer performs math computations only using addition. I understand this because multiplication is a series of additions. Subtraction is like adding the negative value. To do subtraction you have to find the two's complement and then perform the addition. I started getting lost when we got into binary math using signed numbers. I think I understand how to perform multiplication using signed numbers - I think you find the two's complement of the negative, multiply, add, find the two's complement of the result and append the sign bit accordingly. Binary division with signed numbers I don't understand well.

For example,

1111 / 0101

These are signed numbers so the decimal equivalent would be -7 / 5. The answer would be -1R2. If I convert that to signed binary numbers wouldn't that be 1001 0010? So to get there I would have to do 111 - 101 = 010. However, 11 could also be -1, so would I back fill 1s and get 1111 0010? Another question, is about that subtraction - doesn't that need to be changed to addition? If so, wouldn't the problem become 111 + 011 (the twos complement of 010)?

The more I look at it, the more confused I get. Any help would be appreciated.

• If negative, just change the sign before the division, and change it back after.. – Eugene Sh. Nov 22 '16 at 17:09
• First, 1111 is -1, not -7, in 2's compliment – user28910 Nov 22 '16 at 17:10
• 2's complement binary #'s? Then your input of 1111/0101 might be interpreted as -1/5 (decimal), not -7/5. – glen_geek Nov 22 '16 at 17:11
• – Eugene Sh. Nov 22 '16 at 17:12
• @user28910 1111 is not in twos compliment in the original problem. It is a signed binary number. Therefore, the MSB (Most Significant Bit) is acting as magnitude, 0 is positive and 1 is negative. 1111 as a signed binary number is -7. – AxGryndr Nov 22 '16 at 17:15

Signed integer divide is almost always done by taking absolute values, dividing, and then correcting the signs of quotient and remainder, or at least was in earlier CPUs. They may have fancier tricks nowadays. But the fact that dividing by a positive number always truncates toward zero, rather than toward minus infinity, suggests that this is how it's done. In addition to checking for divide by zero, though, it's important to test for dividing the maximum negative number by -1, because that would produce one more than the maximum positive number.

Signed integer multiplies, however, are never done by taking absolute values, multiplying, and then negating if necessary. The difference between a signed integer and an unsigned integer is simply that the msb has a negative weight if it is signed. An unsigned byte has bit weights of 128, 64, 32, 16, 8, 4, 2, and 1. A signed byte has bit weights of -128, 64, 32, 16, 8, 4, 2, and 1. So it's easy to design hardware that takes that into account, using a subtraction instead of an addition when multiplying by the leftmost bit.

Another way of looking at it is that if a byte has a 1 in the msb, then signed value equals the unsigned value minus 256. This means that if you have an unsigned multiplier, you can do a signed multiply pretty easily. If one number has its sign bit set, you subtract the other number from the high half of the result; if the other number has its sign bit set, you subtract the first number from the high half of the result. And if you don't need the high half at all (if you know the numbers are small enough), then there is no difference between signed and unsigned multiply. (I used to do this a lot when I was programming the 6801 and 6809 decades ago.)

BTW, standard floating point representations are always sign-magnitude, rather than two's complement, so they do arithmetic more the way humans do.

A simple answer is to make both numbers positive (take the absolute value), perform the division, then negate the result if the XOR of the two original sign bits is 1.

For example, let's divide -7 by 5. Using 4-bit twos-complement binary encoding, that is 1001 div 0101. Taking the absolute value of each results in 0111 div 0101. The divide yields 0001. Since the XOR of the two original sign bits is 1, this value is negated. Negating means complementing then incrementing by 1. The negative of 0001 is 1111, which is the final answer. 1111 in decimal is -1.

• So are you saying just do 111 / 101 = 1 R 10? To negate the 1 would I get 1111 or 1001? 1111 is the twos complement of 0001 but I started with signed numbers not twos compliment numbers - or does that not matter? – AxGryndr Nov 22 '16 at 17:28
• Absolute value means set sign bit to 0. – stark Nov 22 '16 at 17:48
• @stark Right, which is why I said the math would become 0111 / 0101 but I still don't understand what my final answer would look like. – AxGryndr Nov 22 '16 at 17:51
• @OlinLathrop stark may be right, though. The OP provides "1111 / 0101" and then immediately describes this as -7 / 5. – jonk Nov 22 '16 at 19:58
• @Jon: The OP said a few inconsistent things. Since just about everything out there uses twos complement encoding, that's what I answered. Note that he said "To do subtraction you have to find the two's complement and then perform the addition", which implies twos complement encoding. I think he just got the -7 example wrong. Twos complement is a type of signed number, so the title is no evidence against this. – Olin Lathrop Dec 23 '16 at 11:47