Binary division can be solved using the long division method, which is a method that can teach you the division process yourself as well as to create simple computer programs. Additionally, complementary methods of iterative subtraction can provide approaches you may not be familiar with, even though they are not commonly used for programming. Machine languages usually use approximation algorithms to be more efficient, but this is not described in this article.
Step
Method 1 of 2: Using Long Division
Step 1. Re-learn decimal long division
If you haven't used long division in the regular decimal (base ten) number system in a long time, revisit the basics using the example problem 172 divided by 4. Otherwise, skip this step, and go straight to the next step to explore a similar process with binary numbers.
- Numerator divided by denominator, and the result is quotient.
- Compare the denominator with the first number in the numerator. If the denominator is larger, continue adding numbers to the numerator until the denominator is smaller. (For example, if we calculate 172 divided by 4, we compare 4 with 1, we know that 4 is greater than 1, so proceed to compare 4 with 17.)
- Write the first digit of the quotient above the last numerator used in the comparison. When we compare 4 with 17, we see that 4 is covered by 17 four times, so we write 4 as the first number of the quotient, above 7.
- Multiply and subtract to get the remainder. Multiply the quotient by the denominator, which means 4 × 4 = 16. Write 16 under 17, then subtract 17 by 16 to get the remainder, which is 1.
- Repeat the process. We again compare the denominator, which is 4, with the next number, which is 1, note that 4 is greater than 1, then "subtract" the next number from the numerator, we continue by comparing 4 with 12. We see that 4 is covered by 12 three times no remainder, so we write 3 as the next number of the quotient. The answer is 43.
Step 2. Prepare a long division problem in binary
Let's take 10101 11. Write as a problem for long division, using 10101 as the numerator and 11 as the denominator. Leave space above it as a place to write the quotient, and below it as a place to write calculations.
Step 3. Compare the denominator with the first digit of the numerator
It works the same way as long division in decimal, but it's actually much easier in the binary number system. In binary there are only two options, either you can't divide the number by the denominator (meaning 0) or the denominator is only included once (meaning 1):
11 > 1, so 11 is not "covered by" 1. Write the number 0 as the first number of the quotient (above the first digit of the numerator)
Step 4. Work on the next number and repeat until you get the number 1
Following are the next steps in our example:
- Derive the next number from the numerator. 11 > 10. Write 0 in the quotient.
- Lower the next number. 11 < 101. Write the number 1 in the quotient.
Step 5. Find the remainder of the division
As with long division decimals, multiply the number we just got (1) by the denominator (11), and then write the result under the numerator parallel to the number we just calculated. In the binary number system, we can summarize this process, because 1 x the denominator is always the same as the denominator:
- Write the denominator below the numerator. Here, write 11 parallel to the first three digits of the numerator (101).
- Count 101 - 11 to get the remainder of the division, which is 10. See how to subtract binary numbers if you need to relearn.
Step 6. Repeat until the problem is solved
Decrease the next number from the denominator to the remainder of the division to get 100. Since 11 < 100, write 1 as the next number in the division. Continue the calculation as before:
- Write 11 under 100 and then subtract to get 1.
- Lower the last digit of the numerator to 11.
- 11 = 11, so write 1 as the last digit of the quotient (answer).
- Since there is no remainder, the calculation is complete. The answer is 00111, or 111 only.
Step 7. Add radix points if necessary
Sometimes, the result of a calculation is not an integer. If you still have division left after using the last digit, add ".0" to the numerator and "." to the quotient, so you can still derive one more number and continue the calculation. Repeat until you reach the desired precision, then round the result. On paper, you can round down by removing the last 0, or if the last is a 1, discard it and add the most recent last number to 1. In programming, follow one of several standard rounding algorithms to avoid errors when converting binary numbers to decimal and vice versa.
- Binary division often results in repeated fractional parts, more often than the same process in the decimal system.
- This is more commonly called the "radix point," which applies to any base, because the term "decimal point" applies only in the decimal system.
Method 2 of 2: Using the Complementary Method
Step 1. Understand the basic concept
One way to solve the division problem – on any basis – is to keep subtracting the denominator from the numerator, then the remainder, counting how many times this process can be repeated before getting a negative number. The following example is a calculation in base ten, calculating 26 7:
- 26 - 7 = 19 (subtract 1 time)
- 19 - 7 = 12 (2)
- 12 - 7 = 5 (3)
- 5 - 7 = -2. Negative numbers, so take a step back. The result is 3 and the remainder is divided by 5. Note that this method does not calculate the fractional part of the answer.
Step 2. Learn how to subtract with complements
While you can use the above method in a binary system easily, we can also reduce the use of a more efficient method, which saves time when programming the computer to do binary division. This is subtraction with the complement method in binary. Here are the basics, calculating 111 - 011 (make sure that the two numbers are the same length):
- Find the one's complement for the second number, by subtracting each digit from 1. This step is easy to do in the binary system by changing every 1 to 0 and every 0 to 1. In this example, 011 to 100.
- Add 1 to the result of the calculation: 100 + 1 = 101. This number is called two's complement, so the subtraction can be solved as an addition. In essence, the result of this calculation is like we add negative numbers and not subtract positive numbers, after this process is complete.
- Add the result to the first number. Write and solve the addition problem: 111 + 101 = 1100.
- Remove more numbers. Remove the first number from the calculation result to get the final result. 1100 → 100.
Step 3. Combine the two concepts described above
Now you know the subtraction method for solving division problems, as well as the two's complement method for solving subtraction problems. Using the steps below, you can combine the two into one method to solve the division problem. If you want, try solving it yourself before continuing.
Step 4. Subtract the denominator from the numerator, adding the two's complement
Let's work on the problem 100011 000101. The first step is to solve 100011 - 000101, using the two's complement method to turn this calculation into a sum:
- Two's complement of 000101 = 111010 + 1 = 111011
- 100011 + 111011 = 1011110
- Remove excess numbers → 011110
Step 5. Add 1 to the result of the division
In a computer program, this is where you add 1 to the quotient. On paper, make notes in the corners so they don't get mixed up with other work. We managed to subtract one time, so the result of the division so far is 1.
Step 6. Repeat the process by subtracting the denominator from the remainder of the calculation
The result of our last calculation is the remainder of the division after the denominator is "covered" once. Keep adding the two's complement of the denominator on each repetition and removing the extra digits. Add 1 to the quotient on each iteration, repeating until you get the remainder of the calculation equal to or smaller than the denominator:
- 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 we stop here. The answer to this division process is 111. While the remainder of the division is the final result of the subtraction process, in this case 0 (no remainder).
Tips
- Instructions for raising (adding 1), lowering (subtracting 1), or removing from the stack (pop stack) should be considered before applying binary math in a machine instruction set.
- The two's complement method for subtraction will not work if the numbers have a different number of digits. To fix this, add a zero to the beginning of the number for a smaller number.
- Ignore negative numbers in negative binary numbers before calculating, except to determine whether the answer is positive or negative.