Everyone Can Learn Program
中文 Start learning
Course 0 · Before codeLesson 3 of 8Free to read

Zeros and ones: binary · Computer Basics

A computer only knows "on" and "off". How can that be enough to store numbers?

Course contents

Why you need it

Everything in a computer, from numbers and messages to photos, music and the robot you'll program next, is stored as patterns of on and off, written 1 and 0.

It's the single most important idea about how computers work, and it's simpler than it sounds. By the end of this lesson you'll count in binary.

The intuition

A chip holds billions of tiny switches. Each switch is either off (0) or on (1). One switch is one bit.

One bit can only say two things. But put bits side by side and the patterns multiply:

BitsPatternsHow many
10, 12
200, 01, 10, 114
3000, 001, 010, … 1118
800000000 … 11111111256

Every extra bit doubles the number of patterns. A group of 8 bits is called a byte.

Reading a binary number. In everyday numbers (decimal), each place is worth 10 times the place to its right: ones, tens, hundreds. In binary, each place is worth 2 times the place to its right:

place value8421
bit1101

Add up the place values that have a 1: 8 + 4 + 1 = 13. So binary 1101 means 13.

Picture four light bulbs labelled 8, 4, 2 and 1. To show a number, switch on the bulbs that add up to it. The machine on the right does exactly that: change the bits in quotes and run it.

Definition

Bit

A bit (binary digit) is the smallest unit of data in a computer; it has one of two values, 0 or 1.

Binary

Binary is a way of writing numbers with only the digits 0 and 1, where each place is worth twice the place to its right.

Byte

A byte is a group of 8 bits, enough for 256 different values.

Examples

The binary reader

Try it yourself

Each line shows one bulb, from the biggest place value down to 1. Try 0110, 1000 or 11111111. You don't need to understand the code: only change what's between the quotes.

bits = "1101"   # change me 改这里: only 0 and 1
place = 2 ** (len(bits) - 1)
total = 0
for bit in bits:
    if bit == "1":
        print(place, "on")
        total = total + place
    else:
        print(place, "off")
    place = place // 2
print(bits, "=", total)
Step by step
  1. L1The input: four bits.bits = "1101"
  2. L2Four bits, so the left-most place is worth 8.place = 8
  3. L6The first bit is 1: bulb 8 is on.8 on
  4. L7Add its place value to the total.total = 8
  5. L10Each place to the right is worth half as much: 4, then 2, then 1.place = 4
  6. L11After all four bulbs: 8 + 4 + 1.1101 = 13

Counting in binary

Try it yourself

Watch the right-most bit: it flips every time. The next one flips every 2, the next every 4. Like a car's mileage counter that only has 0 and 1.

how_many = 9   # change me 改这里
for n in range(how_many):
    print(n, "=", format(n, "04b"))
Step by step
  1. L1Count 9 numbers: 0 to 8.how_many = 9
  2. L3Show each number next to its 4-bit binary form.3 = 0011
  3. L38 needs the fourth bulb: 1000.8 = 1000

Each bit doubles it

Try it yourself

Adding one bit doubles the number of patterns. So one byte (8 bits) can store 256 different values, 0 to 255.

print("bits", "patterns")
patterns = 1
for bits in range(1, 9):
    patterns = patterns * 2
    print(bits, patterns)
Step by step
  1. L1A heading for the two columns.bits patterns
  2. L2Before any bits there is just one pattern: nothing.patterns = 1
  3. L4Each new bit doubles it.patterns = 2
  4. L5After 8 bits: eight 2s multiplied together.8 256

Common mistakes

Binary 10 is not ten

In binary, 10 means one 2 and no 1s: two. When you see a number, first ask which system it's written in.

Place values start at the right

The right-most bit is worth 1, then 2, 4, 8 going left. Reading it the other way gives the wrong number: 0001 is 1, not 8.

Zeros in front change nothing

0101 and 101 are both 5, just as 007 and 7 are the same number.

Quick quiz

1. What number is binary 101?

  1. A101
  2. B5
  3. C3
Show the answer

Answer: B

Place values 4, 2, 1. The bits 1, 0, 1 switch on 4 and 1: 4 + 1 = 5.

2. How many different patterns can 3 bits make?

  1. A3
  2. B6
  3. C8
Show the answer

Answer: C

Each bit doubles it: 2 × 2 × 2 = 8 (000 up to 111).

3. Which binary number is 6?

  1. A0110
  2. B0011
  3. C1100
Show the answer

Answer: A

6 = 4 + 2, so the 4 and 2 bulbs are on: 0110. (0011 is 3 and 1100 is 12.)

Key points

  • A bit is one switch: 0 or 1. Eight bits make a byte.
  • Each extra bit doubles the patterns: 8 bits give 256.
  • In binary, places are worth 1, 2, 4, 8 … from the right. Add the places that have a 1.

Practice

3 short exercises: think it through, change something and see what happens, then do one yourself. Answers are checked the moment you submit.

  1. ThinkWhich bulbs are on?

    Four bulbs are labelled 8, 4, 2, 1. They show the number 10. Which bulbs are on?

  2. ChangeMake it 9

    This is the binary reader. Change only the bits between the quotes so the total comes out as 9. Use only 0s and 1s.

    Press Run to check the bulbs, then Submit.

  3. Your turnWrite 12 in binary

    Using the bulbs 8, 4, 2, 1, write the number 12 in binary. Write 1 for each bulb that's on and 0 for each that's off, starting with the 8.

    You can check your answer with the binary reader on the right.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
Bit比特The smallest unit of data: a single 0 or 1.
Byte字节A group of 8 bits; it can hold 256 different values.
Binary二进制Writing numbers with only 0 and 1, each place worth twice the one to its right.
Decimal十进制The everyday number system, with digits 0 to 9 and places worth ones, tens, hundreds.
Place value位值How much a digit is worth because of where it stands in a number.