University of Wales Trinity Saint David
ACCA4021 - Introduction to Computer Systems

Binary Division

Overview

This worksheet shows how a computer divides one binary number by another. You will learn the names for the parts of a division, see why dividing by 2 is just a shift to the right, then use binary long division to divide any two binary numbers by hand. You will check your answers using multiplication, see why dividing by zero cannot work, and divide negative numbers stored in two's complement. There is no programming in this worksheet. Work through it with a pen and paper, and use the interactive diagrams and the Check Answers buttons to test yourself as you go.

Learning Objectives

By the end of this lab you will be able to:

Concept

Division asks how many times one number fits into another. The number being divided is the dividend and the number you divide by is the divisor. The answer is the quotient, and anything left over is the remainder.

In the Binary Multiplication lab you saw that multiplying is just shifting and adding. Division is the opposite, so it is built from shifting and subtracting. Moving every bit one place to the right, called a right shift, halves a number, and the bit that falls off the end is the remainder. For other divisors a computer uses long division, the same method you learned for denary numbers. It is simpler in binary, because at each step the divisor either fits once or does not fit at all. Each quotient bit is therefore 1 or 0, and the only question at each step is is the working value at least as big as the divisor?

The result always obeys one simple rule, dividend = divisor × quotient + remainder, which gives a quick way to check any division using the multiplication you already know.

Tasks

Task 1 - The parts of a division

Here is a denary example. 17 ÷ 5 = 3 remainder 2, because three 5s make 15 and there are 2 left over.

PartMeaningIn 17 ÷ 5
DividendThe number being divided17
DivisorThe number you divide by5
QuotientHow many whole times the divisor fits3
RemainderWhat is left over. It is always smaller than the divisor2

The four parts are always linked by this rule.

dividend  =  divisor×quotient  +  remainder\text{dividend} \;=\; \text{divisor} \times \text{quotient} \;+\; \text{remainder}

For 17 ÷ 5 this is 17 = 5 × 3 + 2. Computers usually work only with whole numbers, so integer division gives a quotient and a remainder rather than a decimal answer like 3.4.

23 ÷ 4. The dividend is and the divisor is

23 ÷ 4 = remainder

40 ÷ 6 = remainder

Check 17 ÷ 5: 5 × + = 17

Which of these could NOT be the remainder when dividing by 6?

The remainder is always smaller than the divisor. If 6 were left over, the divisor would fit one more time.

Task 2 - Dividing by 2 is a right shift

In the Binary Multiplication lab you saw that a left shift doubles a number. A right shift does the opposite. Every bit moves one place to the right, a 0 comes in on the left, and the rightmost bit falls off the end. This halves the number.

The bit that falls off is the 1s column. If it is 1, the number was odd and the remainder is 1. If it is 0, the number was even and the remainder is 0.

StepBinaryBits shifted outMeaning
Start0010110145
Shift right 1 place (÷ 2)00010110145 ÷ 2 = 22 remainder 1
Shift right 2 places (÷ 4)000010110145 ÷ 4 = 11 remainder 1
Shift right 3 places (÷ 8)0000010110145 ÷ 8 = 5 remainder 5

Shifting right by k places divides by 2k. The bits that are shifted out, read as a binary number, are the remainder. You can read both answers straight from the original pattern by drawing a line k places from the right. For 45 ÷ 8 the line goes three places in: 00101 | 101. On the left is the quotient, 5, and on the right is the remainder, 5.

x÷2k  =  x shifted right by k places, remainder = the k bits shifted outx \div 2^{k} \;=\; x \text{ shifted right by } k \text{ places, remainder = the } k \text{ bits shifted out}

Try it on the shifter below. It starts at 45. Press Shift right and watch the bits that fall off collect in the gold tray. After each shift, the tray holds the remainder. You can click any bit to start from a different number.

Write each quotient as an 8-bit binary number and each remainder in denary.

01011010 ÷ 2 (shift right 1 place) = remainder

01011010 ÷ 4 (shift right 2 places) = remainder

00101101 ÷ 8 (shift right 3 places) = remainder

11001000 ÷ 16 (shift right 4 places) = remainder

01001101 ÷ 4 (shift right 2 places) = remainder

11111111 ÷ 32 (shift right 5 places) = remainder

How can you tell, just by looking at its rightmost bit, whether a binary number divides exactly by 2? What about by 4?

A number divides exactly by 2 when its rightmost bit is 0, because that is the bit that would be shifted out as the remainder. It divides exactly by 4 when its two rightmost bits are both 0, because a shift of two places would push out 00, a remainder of 0.

Task 3 - Comparing and subtracting

Long division needs two small skills over and over again. The first is deciding whether one binary number is at least as big as another. The second is subtracting one from the other.

Comparing. Line the two numbers up on the right. Starting from the left, find the first column where they are different. The number with the 1 in that column is the bigger one. For 1010 and 0111, the first difference is in the 8s column, so 1010 (10) is bigger than 0111 (7).

Subtracting. Work from right to left using these rules.

ColumnWrite
0 - 00
1 - 01
1 - 10
0 - 11, and borrow 1 from the next column to the left

Borrowing works because one unit in the next column is worth 2 in this column, so 0 - 1 becomes 2 - 1 = 1. The column you borrowed from then has 1 less in it.

Worked example: 1101 - 0110 (13 - 6)

   1 1 0 1    (13)
 - 0 1 1 0    (6)
   -------
   0 1 1 1    = 7

Right-hand column, 1 - 0 = 1. Next, 0 - 1 needs a borrow from the 4s column, giving 1. The 4s column was 1 but lent 1, so it is now 0, and 0 - 1 needs another borrow, giving 1. The 8s column was 1 but lent 1, so it is now 0, and 0 - 0 = 0. The answer is 0111 = 7.

Which is bigger, 01101 or 01011?

Reading from the left, the first difference is in the 4s column, where 01101 has the 1. 01101 = 13 and 01011 = 11.

Is 1001 at least as big as 0110?

1001 = 9 and 0110 = 6. The first difference is in the 8s column, where 1001 has the 1.

Give each answer as a 4-bit binary number.

1011 - 0110 =

1101 - 0101 =

1001 - 0111 =

1110 - 0011 =

Task 4 - Long division in denary

Before dividing in binary, remind yourself how long division works in denary. Here is 156 ÷ 12.

        0 1 3      quotient
      -------
  1 2 ) 1 5 6
      - 1 2         (15 ÷ 12 = 1, write 1, subtract 12)
        ---
          3 6       (3 left, bring down the 6)
      -   3 6       (36 ÷ 12 = 3, write 3, subtract 36)
          ---
            0       remainder

You move along the dividend one digit at a time. At each step you ask how many times does 12 fit into the working value?, write that digit in the quotient, subtract, and bring down the next digit.

The hard part in denary is the question in the middle. Does 12 go into 36 three times or four times? In binary that question disappears. The divisor either fits once or not at all, so you only ever need to compare and subtract.

Task 5 - Binary long division

To divide two binary numbers, follow these steps.

  1. Write the divisor to the left of the dividend, with a line above the dividend for the quotient.
  2. Start with a working value of 0. Move along the dividend one bit at a time, starting from the left.
  3. Bring down the next bit. That means writing it on the right of the working value, which shifts the working value left and adds the new bit.
  4. Compare. If the working value is at least as big as the divisor, write 1 in the quotient above that bit and subtract the divisor. If not, write 0 and leave the working value alone.
  5. Repeat until every bit has been brought down. What is left in the working value is the remainder.

Worked example: 01001011 ÷ 110 (75 ÷ 6)

      0 0 0 0 1 1 0 0    quotient = 12
     ----------------
110 ) 0 1 0 0 1 0 1 1    (75 ÷ 6)
        - 1 1 0          (9 ≥ 6, so subtract)
        -------
        0 0 1 1 0        (bring down, working value 6)
          - 1 1 0        (6 ≥ 6, so subtract)
            -----
            0 0 0 1 1    remainder = 3

The grey zeros at the front of the quotient are leading zeros. The divisor did not fit at all until step 5. The same division, one step per row, looks like this.

StepBit brought downWorking valueDoes 6 fit?Quotient bitAfter subtracting
100 (0)No, 0 < 60stays 0
211 (1)No, 1 < 60stays 1
3010 (2)No, 2 < 60stays 2
40100 (4)No, 4 < 60stays 4
511001 (9)Yes, 9 ≥ 619 - 6 = 3
60110 (6)Yes, 6 ≥ 616 - 6 = 0
711 (1)No, 1 < 60stays 1
8111 (3)No, 3 < 60stays 3

The quotient is 00001100 = 12 and the remainder is 11 = 3. Check: 6 × 12 + 3 = 75. Correct.

In binary long division, what do you write in the quotient when the divisor fits into the working value?

Bringing down a bit at most doubles the working value and adds 1, so it is always less than twice the divisor. The divisor can never fit twice.

What happens to the working value when the divisor does not fit?

Task 6 - Step through long division

The diagram below carries out binary long division one step at a time on an 8-bit dividend and a 4-bit divisor. It starts with the worked example from Task 5, 75 ÷ 6. Press Next step to bring down the next bit of the dividend. The gold box shows the bit just brought down, and the quotient fills in along the top.

Once you are happy with how it works, click the bits to make your own divisions. Work each one out on paper first, then step through to check.

  1. Set the divisor to 0001. What do you notice about the quotient?
  2. Set the divisor to 0100. Compare the quotient with the dividend. How does this connect to Task 2?
  3. Set the divisor to 0000 and press Show all. What quotient do you get?

Question 2 above. What happens when the divisor is 0100, and why?

The quotient is the dividend shifted right by 2 places, and the remainder is the two bits that would be shifted out. Dividing by 0100 is dividing by 4, which is 22, so long division gives the same answer as a right shift of 2 places.

Question 3 above. What quotient does the stepper give when the divisor is 0000, and why is that answer meaningless?

Every quotient bit is 1, so the quotient is 11111111 (255) and the remainder is the whole dividend. Zero is never bigger than the working value, so the method decides that the divisor fits at every step, and subtracting 0 changes nothing. But no number multiplied by 0 gives the dividend back, so there is no correct answer. Real processors refuse to divide by zero and report an error instead.

Task 7 - A guided division

Work out 01010110 ÷ 0111 (86 ÷ 7) on paper using long division. For each step, write the working value in binary after bringing down the next bit, then the quotient bit for that step.

The first few working values are smaller than 7, so their quotient bits are 0 and nothing is subtracted.

Step 1: bring down a 0. The working value is and the quotient bit is

Step 2: bring down a 1. The working value is and the quotient bit is

Step 3: bring down a 0. The working value is and the quotient bit is

Step 4: bring down a 1. The working value is and the quotient bit is

Step 5: bring down a 0. The working value is and the quotient bit is

Step 6: bring down a 1. The working value is and the quotient bit is

Step 7: bring down a 1. The working value is and the quotient bit is

Step 8: bring down a 0. The working value is and the quotient bit is

The 8-bit quotient is which is in denary

The remainder is in denary

Task 8 - Practice

Use binary long division. Give each quotient as an 8-bit binary number and each remainder as a 4-bit binary number. You can use the stepper in Task 6 to check your working, but try each one on paper first.

00001101 ÷ 0011 = remainder

00101101 ÷ 0101 = remainder

01100100 ÷ 0111 = remainder

10001111 ÷ 1011 = remainder

11001000 ÷ 1100 = remainder

11111010 ÷ 1111 = remainder

Task 9 - Checking with multiplication

Division is easy to get wrong, but easy to check. Multiply the quotient by the divisor using shift and add, then add the remainder. If you get the dividend back, and the remainder is smaller than the divisor, the answer is right.

Worked example. Is 1001011 ÷ 110 = 1100 remainder 11?

1100 × 110 = 12 × 6 = 72. Add the remainder: 72 + 3 = 75 = 1001011. The remainder 3 is smaller than 6. The answer is correct.

Each of the answers below was written by a student. Check each one.

A student says 10110 ÷ 101 = 100 remainder 10. Divisor × quotient + remainder = in denary

The dividend is in denary, so the student is (right or wrong)

A student says 11001 ÷ 100 = 101 remainder 1. Divisor × quotient + remainder = in denary

The dividend is in denary, so the student is (right or wrong)

A student says 101111 ÷ 110 = 111 remainder 101. Divisor × quotient + remainder = in denary

The dividend is in denary, so the student is (right or wrong)

One of the students above made a mistake. Work out the correct answer in binary and explain where they might have gone wrong.

11001 ÷ 100 is 25 ÷ 4 = 6 remainder 1, which is 110 remainder 1. The student wrote 101 (5), which only gives 4 × 5 + 1 = 21. Dividing by 4 is a right shift of two places, so 110 | 01 shows the quotient 110 and the remainder 01 directly. They probably made a mistake with a subtraction or forgot to write a 1 in the quotient at one step.

Task 10 - How big can the answers be?

Multiplication makes numbers grow, so the answer needs more bits. Division never makes numbers bigger. When an n-bit number is divided by a divisor of at least 1, the quotient is no bigger than the dividend, so it always fits in n bits. The remainder is always smaller than the divisor, so it fits in as many bits as the divisor.

This is why processors often divide a big number by a smaller one and give back two results of different sizes, a quotient and a remainder.

The one case that does not work is a divisor of 0. You saw in Task 6 that long division gives a meaningless answer, because no quotient times 0 can ever give back the dividend. Processors check for this and stop the program with a divide by zero error.

An 8-bit number divided by a 4-bit number gives a quotient of up to bits

The same division gives a remainder of up to bits

The biggest possible quotient of an 8-bit number by a 4-bit number is in denary

Dividing by a 4-bit divisor, the biggest remainder possible is in denary

Which divisor gives the biggest possible quotient for a given dividend?

Dividing by 1 gives the dividend itself. Dividing by 0 has no answer at all.

Task 11 - Dividing two's complement numbers

In the Two's Complement lab you stored negative numbers using a -128 column, and found -x with flip and add 1. As with multiplication, the easiest way to divide signed numbers by hand is to deal with the signs separately, using the same sign rule as in denary.

SignsQuotient is
positive ÷ positivepositive
positive ÷ negativenegative
negative ÷ positivenegative
negative ÷ negativepositive
  1. Note whether the quotient will be positive or negative.
  2. Turn any negative number into its positive version with flip and add 1.
  3. Divide the two positive numbers using long division.
  4. If the quotient should be negative, flip and add 1 on the quotient.

Worked example: 11010110 ÷ 00000110 (-42 ÷ 6)

  1. One number is negative, so the quotient is negative.
  2. 11010110 is negative. Flip gives 00101001, add 1 gives 00101010, which is +42.
  3. 42 ÷ 6 = 7 = 00000111.
  4. The quotient should be negative. Flip gives 11111000, add 1 gives 11111001.

Check with the -128 column: -128 + 64 + 32 + 16 + 8 + 1 = -7. Correct.

All the divisions below divide exactly, so there is no remainder to worry about. (When a signed division does leave a remainder, most processors give the remainder the same sign as the dividend.)

Give each quotient in 8-bit two's complement and in denary. Remember the minus sign for negative answers, for example -7.

11110100 ÷ 00000100 = which is in denary

00111000 ÷ 11111000 = which is in denary

10100110 ÷ 11111010 = which is in denary

10011100 ÷ 00000101 = which is in denary

Task 12 - Check your understanding

Shifting a binary number right by 4 places has the same effect as

2 × 2 × 2 × 2 = 16.

What is the remainder when 10110111 is divided by 8?

Dividing by 8 shifts out the three rightmost bits, 111, which is 7.

What is 1111 ÷ 0101?

15 ÷ 5 = 3 exactly.

Why can binary long division never put a 2 in the quotient?

Both are true. The working value is at most twice what was left over (which was less than the divisor) plus 1, so it is less than twice the divisor and the divisor can fit at most once.

Explain why dividing in binary is simpler than dividing in denary.

At each step of long division the divisor either fits once or not at all, so every quotient digit is 1 or 0. You never have to guess how many times the divisor goes in. The only work is comparing the working value with the divisor and subtracting.

A program needs to work out 200 ÷ 16 and the remainder very quickly. Explain how a computer can do this without long division.

16 is 24, so shift 200 = 11001000 right by 4 places. The bits left are 1100 = 12, the quotient, and the 4 bits shifted out are 1000 = 8, the remainder. So 200 ÷ 16 = 12 remainder 8.