Binary and denary

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

F8.2Data representationGCSE15 min

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.

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.

# 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.