Bits & Binary Numbers
Flip a light switch and you already know how computers count. See why machines use 0 and 1, where 255 keeps showing up, and what happens when a game score runs out of room.
- Explain why computers use two symbols instead of ten
- Convert between binary and decimal in both directions
- Add 1 in binary and follow the carry across the bits
- Work out the largest number a given number of bits can hold
Topic contents
Lesson 1 of 3
Two symbols, because that is all electricity offers
Flip a light switch. It is on or off. There is no in-between that the switch can reliably hold. A computer is made of billions of tiny switches like that, and each one is either letting current through or blocking it.
So the machine gets two symbols, 0 and 1, and one of them is called a bit. That word is short for binary digit. A single bit is the smallest yes-or-no answer: light on or light off, door open or door closed.
That sounds limiting until you notice the alphabet. Twenty-six letters look small, yet every story ever written fits inside them. Meaning comes from how you arrange the symbols, not from having hundreds of them.
Binary counter
Add 1 and watch the carry ripple across the bits, read a number off its place values, or convert a decimal number by halving.
Binary numbers
8 bits · 0 to 255Start
Every position is worth double the one to its right: 1, 2, 4, 8 and so on. Reading 00000101 means adding up the places that hold a 1.
Lesson 2 of 3
Counting when you only have two digits
You already know how to run out of digits. In decimal, counting past 9 means resetting the ones column to 0 and carrying into the tens, which is why 9 + 1 is written 10 rather than needing a brand new symbol. Binary does the same thing, just far sooner: it runs out after 1. So 1 + 1 resets that column to 0 and carries left, giving 10, which is binary for two.
Each position is worth double the one to its right: 1, 2, 4, 8, 16, 32, 64, 128. To read a binary number, add up the positions holding a 1. So 101101 is 32 + 8 + 4 + 1, which is 45. Going the other way, halve the decimal number repeatedly and write down each remainder, and those remainders are the bits from right to left.
Open the lab beside this lesson and press Add 1 several times from a value like 7. Watch what happens at 7 going to 8: three bits reset to 0 in a row and a fourth switches on. Nothing is being invented there, it is the carry rule firing three times, and this is exactly the moment students usually assume binary is arbitrary.
Lesson 3 of 3
Eight bits, and the numbers you have seen before
Bits are grouped for convenience, and the common group is eight, called a byte. Eight bits have 2 to the power 8 arrangements, which is 256, so a byte can hold any value from 0 to 255. That is where 255 comes from in colour codes: each of red, green and blue gets one byte, so pure red is 255, 0, 0. It is also where 256 comes from in file formats and palettes.
The same arithmetic explains other familiar numbers. Sixteen bits give 65,536 arrangements, which is why older games capped scores or item counts at 65,535, and why a 16-bit counter wrapping to zero was a common bug. A kilobyte is 1,024 bytes rather than 1,000 because 1,024 is 2 to the power 10, the nearest power of two.
Fixed width has a consequence worth taking seriously. If a value is stored in eight bits and you add 1 to 255, there is no ninth bit for the carry to land in, so the value wraps around to 0. Nothing is broken and no error appears; the number is simply wrong from that moment on. Choose Add 1 in the lab while it shows 11111111 and read what it says.
Worked examples
Read the code, then change it
Copy any example into the playground and break it on purpose. That is the fastest way to learn what each line is holding up.
# Python will show you the bits of any whole number.
for number in [5, 45, 255, 256]:
print(number, "->", bin(number))
# And read them back.
print("101101 is", int("101101", 2))
# Where 255 comes from: every 8-bit pattern, counted.
print("8 bits hold", 2 ** 8, "values, so 0 to", 2 ** 8 - 1)
# The wrap-around, done deliberately.
value = 255
value = (value + 1) % 256
print("255 + 1 in one byte is", value)const bits = '101101';
let total = 0;
for (let i = 0; i < bits.length; i += 1) {
const place = 2 ** (bits.length - 1 - i);
if (bits[i] === '1') {
total += place;
console.log('bit at the ' + place + 's place is 1, total is now ' + total);
}
}
console.log(bits + ' = ' + total);
// Try changing one character above and predict the new total before running it.Practice
Work these out yourself
No answer key here on purpose: these are the questions worth thinking through before you move on. Open one and work it out.
0/3 attemptedAssessment
Check your understanding
Answer each question, then read the explanation. That is where the learning is.
Finished this topic?
0 of 3 lessons marked done. Marking the topic complete ticks the rest.
