Watch on Instagram

Code Bytes · "How Computers Work" · Episode 1 · Bits and bytes

Bits and bytes

A bit is one binary digit, a 0 or a 1, like a tiny switch that is off or on. Every extra bit doubles the number of patterns, and eight bits make a byte, which has 256 patterns.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

Tiny switches: off is 0, on is 1

Inside a computer are huge numbers of tiny switches. Each one is either off or on, nothing in between. We write off as 0 and on as 1.

That's why people say computers run on ones and zeros. Photos, songs, messages and the code you write all end up as long rows of these switches.

A bit is one binary digit

One switch holds one bit. The word is short for binary digit. "Binary" means two choices, and a bit has exactly two: 0 or 1.

Every extra bit doubles the patterns

One bit on its own can only be in 2 patterns:

`` 0 1 ``

Add a second bit and you get 4 patterns, because each of the first bit's patterns can now pair with a 0 or a 1:

`` 00 01 10 11 ``

That's the rule: every extra bit doubles the number of patterns. With n bits you get 2 to the power of n patterns.

Eight bits make a byte

Group 8 bits in a row and you have a byte, the basic unit computers use to measure data (kilobytes, megabytes and gigabytes are all counted in bytes). How many patterns does one byte have? Python can work it out. ** means "to the power of":

``python print(2 ** 8) ``

`` 256 ``

So one byte can be in 256 different patterns, from 00000000 to 11111111.

Seeing binary in Python

The built-in bin() function shows a whole number written in binary:

``python print(bin(5)) ``

`` 0b101 ``

The 0b at the front just means "this is binary". The digits after it, 101, are 5 in binary: one 4, no 2s and one 1 (4 + 0 + 1 = 5).

Python also understands binary when you type it with the 0b in front:

``python print(0b101) ``

`` 5 ``

Patterns plus meaning = anything

A pattern of bits means nothing on its own. It's the meaning we agree to give it that matters. The same byte could stand for a number, a letter or a colour, depending on how a program reads it. That's how ones and zeros can hold anything. Next episode, you'll see how computers use this to store text.

Try it yourself

Open any free online Python editor (search "online Python") and run:

``python print(2 ** 4) print(bin(10)) ``

You should see:

`` 16 0b1010 ``

The first line is the number of patterns 4 bits can make (2 × 2 × 2 × 2 = 16). The second shows 10 in binary: one 8, no 4, one 2 and no 1 (8 + 0 + 2 + 0 = 10). Try bin() on your age or your favourite number.

Good to know

  • Every snippet on this page was run with Python 3 (3.13) and printed exactly what is shown.
  • "Tiny switches" is a simplification. In modern chips the switches are transistors, and a bit can also be stored as an electric charge, a tiny magnetic area or a pit on a disc.
  • A byte is 8 bits in every modern computer. Some very old machines used other sizes.
  • bin() gives back text (a string) that starts with 0b. If you write 0b101 in your code without quotes, Python reads it as the number 5.
  • With n bits you get 2 to the power of n patterns, so it's quick to check with 2 ** n in Python.
  • Network Bytes EP19 goes deeper on counting in binary.

Answer to the comment question

How many patterns can three bits make?

8

Each extra bit doubles the patterns: 1 bit gives 2, 2 bits give 4, so 3 bits give 8. That's 2 to the power of 3, and print(2 ** 3) prints 8. Here they all are:

`` 000 001 010 011 100 101 110 111 ``

Test your knowledge

Quick quiz: Bits and bytes

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What is a bit?
2What happens to the number of patterns when you add one more bit?
3How many patterns can one byte make?
4What does print(bin(5)) print?

Sources

  • Britannica, "bit": a bit, short for binary digit, is the smallest unit of information in a computer and has one of two values, 0 or 1
  • Britannica, "byte": a byte is a group of bits, today almost always 8, used as a basic unit of data
  • Python Software Foundation, "Built-in Functions": bin(): converts an integer to a binary string prefixed with "0b"
  • Python Software Foundation, "Lexical analysis": Integer literals: 0b marks a binary number, so 0b101 is 5

Video transcript

How can everything on your computer be just ones and zeros?

Inside, a computer is full of tiny switches, each off or on. Off is zero, on is one.

One switch is a bit, short for binary digit. Binary means two choices.

One bit gives two patterns. Two bits give four. Every extra bit doubles the patterns.

Eight bits in a row make a byte. That's two to the power of eight: two hundred fifty-six patterns.

In Python, bin of five shows five in binary: zero b, one zero one. Zero b just means binary.

Give each pattern a meaning, like a number, a letter or a colour, and bits can be anything.

How many patterns can three bits make? Comment your answer below!

That's your byte.

More from "How Computers Work"

All "How Computers Work" lessons