Dividing binary numbers

Author: Roger Morrison
Date Of Creation: 6 September 2021
Update Date: 1 July 2024
Anonim
Binary Division
Video: Binary Division

Content

Dividing binary numbers can be solved by using long division, a handy method for teaching yourself the procedure or writing a simple computer program. Alternatively, the complement method of repeated subtraction offers an approach that you may not be familiar with, although not really commonly used in programming. Machine languages ​​usually use an estimation algorithm for greater efficiency, but these are not described here.

To step

Method 1 of 2: Using long division

  1. Go through the decimal long division again. If it has been a while since you have done long division with regular decimal (base 10) numbers, go over the base of it again for the problem 172 ÷ 4. Otherwise, skip this and go to the next step to learn this procedure for binary numbers.
    • It dividend is divided by the divisor, and the answer is it quotient.
    • Compare the divisor with the first digit in the dividend. If the divisor is the largest number, keep adding digits to the dividend until the divisor is the smallest number. (For example, when calculating 172 ÷ 4, we compare 4 and 1, find 4> 1, and then compare 4 with 17.)
    • Write the first digit of the quotient above the last digit of the dividend used for the comparison. After comparing 4 and 17, we notice that 4 goes into 17 four times, so we write 4 as the first digit of our quotient, above 7.
    • Multiply and subtract to find the remainder. Multiply the quotient by the divisor, in this case 4 x 4 = 16. Write the 16 below 17, then do 17 - 16 for the remainder, 1.
    • Repeat. Again we compare the divisor 4 with the next digit, 1, notice that 4> 1, and "bring" down the next digit of the dividend, to compare 4 with 12 instead. 4 goes into 12 three times with no remainder, so we can write 3 as the next digit of the quotient. The answer is 43.
  2. Create a binary long division setup. Suppose we use 10101 ÷ 11 as an example. Write this as a long division, with 10101 as the dividend and 11 as the divisor. Leave space above to write the quotient, and write your calculations below.
  3. Compare the divisor with the first digit of the dividend. This works in the same way as decimal long division, but is actually a lot easier in binary form. Or you can't divide the number by the divisor (0), or the divisor fits in once (1):
    • 11> 1, so 11 "does not fit" 1. Write a 0 as the first digit of the quotient (above the first digit of the dividend).
  4. Now take the next digit and repeat until you get 1. Here are the next few steps from our example:
    • Bring down the next digit of the dividend. 11> 10. Write a 0 in the quotient.
    • Bring down the next digit. 11 101. Write a 1 in the quotient.
  5. Determine the rest. as in a decimal long division, we multiply the digit we just found (1) by the divisor (11), and write the result below our dividend on a line with the digit we just calculated. In binary form we can do this faster, because 1 x the divisor is always equal to the divisor:
    • Write the divisor below the dividend. Here we write this as 11 under the first three digits (101) of the dividend.
    • Calculate 101 - 11 for the rest, 10. Review how to subtract binary numbers if you don't remember.
  6. Keep going until the problem is resolved. Bring the next digit from the divisor to the rest below to get 100. Because 11 100, you write a 1 as the next digit of the quotient. Continue to work out the problem as before:
    • Write 11 below 100 and subtract these numbers to get 1.
    • Bring down the last digit of the dividend and you will get 11 for the answer.
    • 11 = 11, so write 1 as the last digit of the quotient (the answer).
    • There is no remainder, so the problem is completed. The answer is 00111, or more simply, 111.
  7. Add a radix point if necessary. Sometimes the result is not an integer. If you still have a remainder after using the last digit, add a ".0" to the dividend and a "." to your quotient so you can bring one more number down and move on. Keep doing this until you reach your desired accuracy, then finalize your answer. On paper you can round off by omitting the 0 or, if the last digit is a 1, removing it and adding 1 to the last digit. When programming, use one of the standard rounding algorithms to avoid errors when converting between binary and decimal numbers.
    • Dividing binary numbers often results in repeating decimal places, more often than those occurring in decimal format.
    • This is referred to by the more general term "radix point" that you encounter in any number system, because you encounter the "decimal point" only within the decimal system.

Method 2 of 2: Using the complement method

  1. Understand the basic idea. One way to solve divisions - for any base - is to keep subtracting the divisor from the dividend, then the remainder, counting how many times you can keep doing this before you get to a negative number. . Here is an example for the base 10, the problem 26 ÷ 7:
    • 26 - 7 = 19 (subtracted 1 time)
    • 19 - 7 = 12 (subtracted 2 times)
    • 12 - 7 = 5 (subtracted 3 times)
    • 5 - 7 = -2. Negative number, so up again. The answer is 3 with a remainder of 5. Note that this method does not consider decimal places.
  2. Learn to subtract using complements. While you can easily apply the above method to binary numbers, we can also use a more efficient method that will save you time when programming binary divisions. This is called the binary complement method. Here is the basis, calculating 111 - 011 (make sure both numbers are the same length):
    • Find the complement of the ones of the second term, subtracting each digit from 1. You can easily do this with binary numbers by setting every 1 to 0 and every 0 to 1. In our example, 011 becomes 100.
    • Add 1 to the result: 100 + 1 = 101. This is called the 2's complement. We will now consider a subtraction as an addition. The essence is that we treat the problem as if we were adding a negative number, instead of subtracting a positive number, after completing the procedure.
    • Add the result to the first term. Solve the addition: 111 + 101 = 1100.
    • Omit the first digit (carry digit). Remove the first digit from your answer to get the final result. 1100 → 100.
  3. Combine the two concepts above. Now you know how the subtraction method for solving division sums works and the 2's complement method for solving subtraction sums.You can combine the two into one method of solving division sums using the steps below. If you want, you can try to figure it out for yourself before continuing.
  4. Subtract the divisor from the dividend by adding the 2's complement. Let's do the problem: 100011 ÷ 000101. The first step is to solve 100011 - 000101, using the 2's complement method, so that it adds up:
    • 2's complement of 000101 = 111010 + 1 = 111011
    • 100011 + 111011 = 1011110
    • Omit the first digit (the carry) → 011110
  5. Add 1 to the quotient. In a computer program, this is the point where you increase the quotient by 1. On paper, make a note somewhere in a corner where it won't mess up the rest of your work. We've successfully done a subtraction once, so the quotient so far is 1.
  6. Repeat this by subtracting the divisor from the remainder. The result of our last calculation is the remainder that is left after the divisor "goes in" once. Continue adding the divisor's 2's complement and subtracting the carry. Add 1 to the quotient each time, and continue until you get a remainder equal to your smaller divisor:
    • 011110 + 111011 = 1011001 → 011001 (quotient 1 + 1 = 10)
    • 011001 + 111011 = 1010100 → 010100 (quotient 10 + 1 = 11)
    • 010100 + 111011 = 1001111 → 001111 (11+1=100)
    • 001111 + 111011 = 1001010 → 001010 (100+1=101)
    • 001010 + 111011 = 10000101 → 0000101 (101+1=110)
    • 0000101 + 111011 = 1000000 → 000000 (110+1=111)
    • 0 is less than 101, so now we can stop. The quotient 111 is the answer to the partial problem. The remainder is the final result of our subtraction, in this case 0 (no rest).

Tips

  • The increase, decrease, or stack instructions should be considered before applying a binary calculation to a set of machine instructions.
  • The 2's complement subtraction method does not work if the numbers consist of a different number of digits. Add extra zeros to the smaller number to solve this.
  • Ignore the signed digit in signed binary numbers before making the calculation, except when trying to determine whether an answer is positive or negative.