Basic

What a bit is

A bit is one switch with two positions. Everything a machine stores is a row of these switches, because a machine can only tell one thing from another.

What you will see

0b1000001

About 2 min of reading

Try It

Probe what bits can hold.

1

Print the two values one bit holds.

print(2 ** 1)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
2
2

Print what eight bits hold.

print(2 ** 8)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
256
3

Read a row of bits as a number.

print(int("1100", 2))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
12
4

Show the bits of sixty five.

print(bin(65))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
0b1000001
The 0b prefix marks a binary literal, it is not part of the value.

Data Flow

A bit is one two-state switch, and a row of bits can represent many more values than a single one.
1 one bit holds 2 values
↓
2 two bits hold 4
↓
3 eight bits hold 256
↓
4 each bit doubles the count
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

print(2 ** 1) 2
Why this works
Two states are buildable

A machine is built from parts that answer one of two things. Two states is the simplest thing to build reliably, and combinations of it can represent anything.

Rows have meaning

The same row of bits can be a number, a letter, or a color. What it means depends on the rule used to read it, which is the point of every later lesson.

Examples

Two states

print(0)
print(1)

A bit is 0 or 1, off or on, yes or no. Those two answers are all a single bit can carry.

A row of bits

print(0b1011)

Four bits in a row read as eleven. The row is a number, and the same row can be a letter or a color.

Counting what bits hold

print(2 ** 1, 2 ** 2, 2 ** 8)

One bit holds two values, two bits hold four, eight bits hold two hundred fifty six. Each bit doubles the count.

Bits to a number

print(int("1100", 2))

Reading the row of ones and zeros as base two gives twelve. The row is text, the number is what it means.

A byte is eight bits

print(len(bin(255)) - 2)

The largest value in one byte needs eight bits. A byte is the row of eight switches a machine handles as one unit.

Bits are not text

print(bin(65))

The number sixty five is stored as bits. The letter A is a rule that maps those bits to a shape on screen.

Why bits are enough

print(0b11111111)

Eight switches answer two hundred fifty six questions. That is enough to name every plain letter and small number.

A switch is a choice

print(1 - 0)

One bit records one yes-or-no answer. A row of bits records many such answers side by side.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
Counting in binary
Counting in binary →