How to divide binary numbers

Author: Mark Sanchez
Date Of Creation: 2 January 2021
Update Date: 1 July 2024
Anonim
Binary Division
Video: Binary Division

Content

Binary numbers can be divided into columns to better understand the process itself, or to write a simple computer program. You can also use the complement method, which is rarely used in programming. Typically, machine languages ​​use a scoring algorithm to be more efficient, but this is not what this article is about.

Steps

Method 1 of 2: Long Division

  1. 1 Divide into a column two decimal numbers. If you've forgotten long division, divide two decimal (base 10) numbers: 172 ÷ 4. If long division is great, go to the next step to learn how to divide binary numbers.
    • Dividend divided by divider and it turns out private.
    • Compare the divisor with the first digit of the dividend. If the divisor is greater than this digit, compare the divisor with two digits of the dividend, and so on, until the divisor is less than the number in question. In our example, compare 4 and 1, note that 4> 1, and then compare 4 with 17.
    • Write the first digit of the quotient below the divisor. Comparing 4 and 17, you will see that 17 ÷ 4 = 4 with remainder, so write 4 as the first digit of the quotient below the divisor (4).
    • Multiply and subtract to find the remainder. Multiply the first digit of the quotient by the divisor; in our example: 4 x 4 = 16. Write 16 under 17, then subtract 17 - 16 to find the remainder of 1.
    • Repeat the comparison. Compare the divisor 4 with the remainder of 1, note that 4> 1, and "carry" the next digit of the dividend to compare 4 with 12. Since 12 ÷ 4 = 3 without a remainder, so write 3 as the second digit of the quotient. The final answer is 43.
  2. 2 Column divide two binary numbers. For example, 10101 ÷ 11. Here 10101 is the dividend and 11 is the divisor. Leave enough room for calculations.
  3. 3 Compare the divisor with the first digit of the dividend. In the case of binary numbers, this is easier to do than with decimal numbers: either the number is not divisible by the divisor and we write 0, or it is divided and we write 1.
    • 11> 1, so 1 cannot be divided by 11. Write 0 as the first digit of the quotient (below the divisor).
  4. 4 Keep comparing the divisor numbers until you get 1. In our example:
    • Compare the divisor with the two digits of the dividend. 11> 10. Write 0 as the second digit of the quotient.
    • Compare the divisor with the three digits of the dividend. 11 101. Write 1 as the third digit of the quotient.
  5. 5 Calculate the remainder. Multiply the found digit (1) by the divisor (11) and write the result under the dividend (namely, under the corresponding digits). Note that multiplying 1 by a divisor always results in a divisor.
    • Write the divisor under the dividend. In our example, write 11 under the first three digits (101) of the dividend.
    • Subtract 101 - 11 to get a remainder of 10. If you don't remember how to subtract binary numbers, read this article.
  6. 6 Repeat the described steps until you solve the problem. Add the next digit of the dividend to the remainder to get 100. Since 11 100, write 1 as the fourth digit of the quotient. Further calculations:
    • write 11 under 100 and subtract to get a remainder of 1;
    • add the last digit of the dividend to the remainder to get 11;
    • 11 = 11, so write 1 as the last digit of the quotient.
    • There is no remainder, so the problem is solved. Answer: 00111 or just 111.
  7. 7 Add a decimal point (if needed). Sometimes the result is not an integer. If after you have used the last digit of the dividend, you get a remainder, add ", 0" to the dividend and "," to the quotient, to "demolish" the next digit and continue the calculation. Repeat this process until you get the result you want, and then round up your answer. To round your result, get rid of the last 0, or if the last digit is 1, drop it and add 1 to the new last digit. When programming, follow one of the standard rounding algorithms to avoid errors when converting between binary and decimal numbers.
    • Dividing two binary numbers can result in a repeating fractional part; this happens more often than when dividing decimal numbers.
    • Please note that the decimal point is used not only in decimal, but also in binary notation.

Method 2 of 2: Supplements

  1. 1 Understand the basic principles. To divide two numbers (both decimal and binary), you can subtract the divisor from the dividend and then successively subtract the divisor from the remainders until you get a negative number; in this case, you need to count how many subtractions have been performed. For example, calculate 26 ÷ 7:
    • 26 - 7 = 19 (1 subtraction)
    • 19 - 7 = 12 (2)
    • 12 - 7 = 5 (3)
    • 5 - 7 = -2. A negative number, so you don't need to subtract further. Answer: 3 with a remainder of 5. Note that this method does not calculate the fractional part of the answer.
  2. 2 Understand the basics of the addition method. The above method can be applied to binary numbers, or you can use a more efficient method that saves time when programming the division of binary numbers. This method is called the complement method. For example, subtract 111 - 011 (both numbers must have the same number of digits):
    • Find the complement to the second number. To do this, subtract each digit of this number from 1. In binary, just replace 1 with 0, and 0 with 1. In our example, 011 becomes 100.
    • Add 1: 100 + 1 = 101 to your result. This process is called two's complement and allows you to replace subtraction with addition. Basically, this method is that you add a negative number instead of subtracting a positive one.
    • Add the result to the first number. Write down and calculate the addition operation: 111 + 101 = 1100.
    • Drop the first digit of your result to get the final answer: 1100 → 100.
  3. 3 Combine the two methods described above. The first method is the sequential subtraction method and the second is the two's complement method. These methods can be combined into one to use it to divide numbers (the process of combining methods is described below). If you want, try to figure out how to combine the two methods yourself.
  4. 4 Subtract the divisor from the dividend, replacing subtraction with two's complement addition. For example: 100011 ÷ 000101.First, turn the subtraction 100011 - 000101 into addition using two's complement:
    • Two's complement: 000101 = 111010 + 1 = 111011
    • Addition: 100011 + 111011 = 1011110
    • Get rid of the first digit: 011110
  5. 5 Add 1 to the quotient. In a computer program, this is a string where the quotient is incremented by one. Make a note on paper to avoid confusion. You have successfully subtracted once, so the quotient is 1 at this point.
  6. 6 Repeat the described process. To do this, subtract the divisor from the remainder. The remainder is the result of the last calculation. Replace the subtraction operation with addition: add the two's complement divisor to the remainder, and then get rid of the first digit of the result. After each subtraction, add 1 to the quotient. Repeat the above process until the remainder is equal to or less than the 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 there is no need to calculate further. Private 111 is the final result of the division operation. The remainder is the final result of the subtraction operation; in our example it is 0 (no remainder).

Tips

  • Ignore the sign bit in signed binary numbers unless you need to know if the result is positive or negative.
  • The two's complement method does not apply if the numbers contain different numbers of digits. In this case, add the corresponding number 0 to the lower number (on the left).
  • The instructions to increase, decrease, or pop the stack must be considered before applying binary operations to machine instructions.