Binary and denary

Converting 8-bit numbers both ways, and hearing them on the buzzer.

F8.2Data representationGCSE15 min

Covers OCR J277 1.2.4 Data storage · AQA 8525 3.3.1 Number bases · AQA 8525 3.3.2 Converting between number bases · Edexcel 1CP2 2.1.3 Denary and 8-bit binary

Do this lesson in the simulator

We count in denary (base 10): ten digits, and each place is worth ten times the one to its right. Computers count in binary (base 2): two digits, and each place is worth twice the one to its right. Converting between them is a skill every exam tests, and BugBot can play a number to you in binary.

Place values

In an 8-bit binary number, the places are worth:

128 64 32 16 8 4 2 1
0 1 0 0 1 1 0 1

To turn binary into denary, add up the place values where there is a 1: 64 + 8 + 4 + 1 = 77.

The largest 8-bit number, 11111111, is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, so 8 bits can store the 256 values from 0 to 255.

The eight place values double from right to left. With every bit a 1 they add up to 255, one less than 256, the value a ninth place would havea ninth place2561286432168421× 2× 2× 2× 2× 2× 2× 225511111111128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255one less than 256, so 8 bits hold 256 values: 0 to 255
Each place is worth twice the one on its right, just as each denary place is worth ten times. Fill all eight places with 1s and the total is 255, one short of the 256 a ninth place would be worth.

Binary to denary in Python

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def to_denary(bits):
    """'01001101' -> 77, using place values."""
    total = 0
    value = 1
    for bit in reversed(bits):
        if bit == "1":
            total = total + value
        value = value * 2
    return total

print(to_denary("01001101"))
print(to_denary("11111111"))
print(int("01001101", 2))          # Python's own way, to check

Run this in the simulator

The loop starts at the right-hand bit, worth 1, and doubles the place value each time it moves left.

Denary to binary

Two methods, and both give the same answer. Subtracting place values: for each place from 128 down, if the number is at least that value, write 1 and take it away; otherwise write 0. Dividing by 2: divide by 2 again and again, writing down the remainders, then read them from the last to the first.

77 turned into binary by taking away place values: 64, 8, 4 and 1 fit, which gives 01001101placedoes it fit into what is left?bit128128 > 77: no06464 ≤ 77: yes, 77 − 64 = 13 left13232 > 13: no01616 > 13: no088 ≤ 13: yes, 13 − 8 = 5 left144 ≤ 5: yes, 5 − 4 = 1 left122 > 1: no011 ≤ 1: yes, 1 − 1 = 0 left1read77 =01001101
Work down from 128. Where the place value fits into what is left, write 1 and take it away; where it does not, write 0. When nothing is left, every place below gets a 0.
77 turned into binary by dividing by 2: the remainders, read from the last to the first, are 100110177 ÷ 2 = 38remainder138 ÷ 2 = 19remainder019 ÷ 2 = 9remainder19 ÷ 2 = 4remainder14 ÷ 2 = 2remainder02 ÷ 2 = 1remainder01 ÷ 2 = 0remainder1read theremaindersupwardsstop at 0, thenadd a 0 in frontto make 8 bits77 =01001101
The first remainder is the ones bit, the right-hand end of the answer, so the remainders are read from the bottom up. Both methods give 01001101.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def to_binary(n):
    """77 -> '01001101', by taking away place values from 128 down."""
    bits = ""
    for value in [128, 64, 32, 16, 8, 4, 2, 1]:
        if n >= value:
            bits = bits + "1"
            n = n - value
        else:
            bits = bits + "0"
    return bits

for n in [77, 5, 200, 255]:
    print(n, "=", to_binary(n), "check:", format(n, "08b"))

Run this in the simulator

format(n, "08b") is Python's own conversion: 8 digits, binary, with leading zeros.

Hear a number

Here the robot plays 77 in binary: a high note for each 1, a low note for each 0.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

bits = format(77, "08b")
print(bits)
for bit in bits:
    if bit == "1":
        tone(880, 0.2)
    else:
        tone(440, 0.2)
    wait(0.1)

Run this in the simulator

Change 77 to another number between 0 and 255 and listen to its pattern.

Task: play in binary

Write to_binary(n) yourself, without bin, format or int(..., 2), that returns an 8-character string of 1s and 0s. Use it to print the binary of 77, 5 and 200, one per line, and then play the bits of 77: a note of 880 Hz for each 1 and 440 Hz for each 0, each for 0.2 seconds.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def to_binary(n):
    return ""

Challenges

  1. How many bits are needed for the number 1,000? Write a loop that doubles until it passes 1,000.
  2. Convert your age to binary by hand, then check with your function.
  3. Write to_denary with the dividing-by-2 idea turned round: start from the left, and for each bit double the total and add the bit.

Past paper questions on this

Real exam questions that test this, each worked through with a program you can run.