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.

  • 1
    \$\begingroup\$ If negative, just change the sign before the division, and change it back after.. \$\endgroup\$ – Eugene Sh. Nov 22 '16 at 17:09
  • \$\begingroup\$ First, 1111 is -1, not -7, in 2's compliment \$\endgroup\$ – user28910 Nov 22 '16 at 17:10
  • \$\begingroup\$ 2's complement binary #'s? Then your input of 1111/0101 might be interpreted as -1/5 (decimal), not -7/5. \$\endgroup\$ – glen_geek Nov 22 '16 at 17:11
  • \$\begingroup\$ anyway. stackoverflow.com/questions/20793701/… \$\endgroup\$ – Eugene Sh. Nov 22 '16 at 17:12
  • \$\begingroup\$ @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. \$\endgroup\$ – AxGryndr Nov 22 '16 at 17:15

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.

  • \$\begingroup\$ 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? \$\endgroup\$ – AxGryndr Nov 22 '16 at 17:28
  • \$\begingroup\$ Absolute value means set sign bit to 0. \$\endgroup\$ – stark Nov 22 '16 at 17:48
  • \$\begingroup\$ @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. \$\endgroup\$ – AxGryndr Nov 22 '16 at 17:51
  • \$\begingroup\$ I suppose -7/5 = -1, which is what this answer says. The two sign bits 1 xor 0 = 1, so the result is negative = 1001. \$\endgroup\$ – stark Nov 22 '16 at 17:57
  • 2
    \$\begingroup\$ @OlinLathrop stark may be right, though. The OP provides "1111 / 0101" and then immediately describes this as -7 / 5. \$\endgroup\$ – jonk Nov 22 '16 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.