Grade by GradeEssential student suppliesSign in
Applied Design, Skills, and Technologies · Design and Build: Grade 8 Designers, Builders and Coders

Computational Thinking: Binary Data, Debugging and Text-Based Code

BC ADST 8: binary number system BC ADST 8: debugging algorithmsAbout 45 minutes
Learning goals
  • I can use the binary number system to represent numbers, text and colours.
  • I can debug an algorithm or program by breaking the problem into sub-problems.
  • I can read short text-based programs, including HTML and Python, and explain what they do.

Words to know

bit
A binary digit: a single 0 or 1.
byte
A group of 8 bits. One byte can store 256 different values, 0 to 255.
ASCII
A standard code that gives each letter, digit and symbol a number, so text can be stored in binary.
algorithm
A set of specific steps, in order, that solves a problem and can be repeated reliably by others.
decomposition
Breaking a big problem into smaller sub-problems that are easier to solve or test.
HTML
HyperText Markup Language: text-based code that tells a web browser how to structure a web page.

Everything is 0s and 1s

Inside a computer, all data is stored as bits: tiny switches that are off (0) or on (1). Eight bits make a byte.

In the binary number system, each place is worth twice the place to its right: 1, 2, 4, 8, 16, 32, 64, 128. With 8 places, the biggest number is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, so one byte can store 256 values (0 to 255).

Computers use binary to represent much more than numbers:

  • Text: codes such as ASCII give each character a number. Capital A is 65, which is 01000001.
  • Colours: many screens mix red, green and blue light. Each colour gets one byte (0 to 255), so one pixel uses 24 bits. Pure red is (255, 0, 0). That gives 256 x 256 x 256 = 16,777,216 possible colours.
  • Images, sound and video are long lists of numbers, so they are binary too.
place value12806413201608141201164 + 8 + 4 + 1 = 77, the code for the letter M
The byte 01001101. Add the place values of the 1s: 64 + 8 + 4 + 1 = 77. In ASCII, 77 is capital M.
CharacterDecimal codeBinary (1 byte)
space3200100000
0 (the digit)4800110000
A6501000001
B6601000010
M7701001101
a9701100001
Some ASCII codes. Notice that lower-case a is exactly 32 more than capital A.
Worked example

Decoding a secret message

Decode the two bytes 01001000 01101001 using ASCII. Then write 45 in 8-bit binary.

  1. First byte, 01001000: the 1s are in the 64 and 8 places. 64 + 8 = 72. ASCII 72 is H.
  2. Second byte, 01101001: the 1s are in the 64, 32, 8 and 1 places. 64 + 32 + 8 + 1 = 105. ASCII 105 is i (lower-case a is 97, so 105 is 8 letters later: a, b, c, d, e, f, g, h, i).
  3. Writing 45: take away the biggest place value that fits. 45 - 32 = 13. 13 - 8 = 5. 5 - 4 = 1. 1 - 1 = 0.
  4. Put 1s in the 32, 8, 4 and 1 places and 0s elsewhere: 0 0 1 0 1 1 0 1.

The message says Hi, and 45 = 00101101.

Algorithms and debugging

A program is a set of specific, sequential instructions: an algorithm that anyone (or any computer) can follow and get the same result every time.

When a program doesn't work, a strong strategy is decomposition: break the problem into sub-problems and test each one separately. Instead of staring at the whole program, ask: Is the input right? Is this step right? Is the next step right? The first step that gives a wrong result is where the bug lives.

Text-based code

Visual (block) languages show code as blocks. Text-based languages are typed, so spelling, capital letters, punctuation and spacing matter. Below are two examples: Python, a general-purpose language, and HTML, which structures web pages.

scores = [7, 9, 6, 10]
total = 0
for s in scores:
total = total + s
average = total / 3
print("Average:", average)

A Python program meant to find the average quiz score. It runs, but it prints the wrong answer. Can you find the bug?

Worked example

Debugging with sub-problems

The program above prints Average: 10.666666666666666, but the average of 7, 9, 6 and 10 should be 8. Find and fix the bug by testing sub-problems.

  1. Sub-problem 1, the input: the list has 4 scores: 7, 9, 6, 10. Correct.
  2. Sub-problem 2, the total: add a test line print(total) after the loop. It shows 32, and 7 + 9 + 6 + 10 = 32. Correct.
  3. Sub-problem 3, the average: 32 / 3 = 10.666... The code divides by 3, but there are 4 scores. Bug found.
  4. Fix: change the line to average = total / len(scores). len(scores) counts the items (4), so the code still works if more scores are added.
  5. Test again: 32 / 4 = 8.0, and the program prints Average: 8.0.

The bug was dividing by 3 instead of the number of scores. Using len(scores) fixes it.

<!DOCTYPE html>
<html>
<head>
<title>Pacific Salmon</title>
</head>
<body>
<h1>Pacific Salmon in B.C.</h1>
<p>Five species spawn in B.C. rivers:</p>
<ul>
<li>Chinook</li>
<li>Coho</li>
<li>Sockeye</li>
<li>Pink</li>
<li>Chum</li>
</ul>
</body>
</html>

A short HTML page. Most tags come in pairs: an opening tag and a closing tag with a slash. The browser shows a heading, a sentence and a bulleted list of five salmon species.

Print to peek inside

Adding a temporary print() line (or a "say" block in a visual language) shows the value of a variable at that point. It is like opening a box to check what's inside. Remove the test lines when the bug is fixed.

Hands-on activity

Binary bracelet and bug hunt

  1. Write your first name in ASCII binary (use the table and count up from A = 65 or a = 97).
  2. Make a bracelet or bookmark: one colour of bead or marker dot for 1 and another for 0, one byte per letter.
  3. With an adult's permission, open a free online Python editor approved by your school. Type the average program, run it, and fix the bug.
  4. Change the list to five scores of your own and check the average with a calculator.
  5. Bonus: write a short HTML page about a B.C. animal with a heading, a paragraph and a list.
Joke

Why do programmers mix up Halloween and Christmas?

Because Oct 31 = Dec 25. (In base 8, called octal, 31 means 3 x 8 + 1 = 25 in our usual base 10.)

Let's practise

1

Convert 00011001 to a decimal number.

2

Write 200 in 8-bit binary.

3

What is the largest number one byte can store?

4

Capital C comes after B in ASCII. What is its code in decimal and binary?

5

How many bits are in 3 bytes?

6

What is decomposition, and why does it help debugging?

More practice

1

Decode 01000010 01000011 in ASCII.

2

How many different values can 4 bits store?

3

In the RGB system, what colour is (0, 0, 255)?

4

A web page shows everything after one list item as part of that item. What HTML mistake might cause this?

5

What does len(scores) give for scores = [5, 8, 9]?

Check your understanding

Quiz

1.

What is 00010110 in decimal?

2.

How many bits are in one byte?

3.

A program's total is correct but its average is wrong. Using decomposition, where should you look next?

4.

Which is text-based code that structures a web page?

For parents and teachers

How to help at home: Send each other short messages in ASCII binary. When your child is stuck on a coding bug, ask "Which part works? Which part doesn't?" instead of fixing it for them. Use only coding sites approved by the school.

Common mistakes: Reading binary place values from the wrong end, forgetting that the place values double from right to left, dividing by a fixed number instead of counting items, and missing closing tags or colons in text-based code.