Basic

Counting in binary

Binary is a number system with only two digits. Each position is a power of two, and a number is the sum of the powers that are switched on.

What you will see

10

About 2 min of reading

Try It

Count and read binary rows.

1

Print the first eight numbers in binary.

for i in range(8): print(bin(i))

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

What it prints
0b0 through 0b111
2

Read a row as a number.

print(int("10110", 2))

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

What it prints
22
3

Show all four ones.

print(0b1111)

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

What it prints
15
4

Check that ten is eight plus two.

print(0b1010)

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

What it prints
10

Data Flow

Binary numbers are sums of powers of two, and each position is worth double the one to its right.
1 each position doubles
↓
2 a 1 switches a power on
↓
3 all ones is one less than next power
↓
4 counting carries left
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.

for i in range(8): sets nothing on screen
print(bin(i)) 0b0 through 0b111
Why this works
Why base two

A machine only has two states, so base two needs no translation. Decimal would have to be built out of bits, which is what binary already is.

Carrying works the same

Adding in binary carries when a column gets two, exactly as decimal carries at ten. The mechanics you know already apply.

Examples

Powers of two

print(0b1000)
print(0b100)
print(0b10)

A single 1 followed by zeros is a power of two: 8, 4, 2. Binary is those powers added together.

Add the switched powers

print(0b1010)

That row has the 8 and the 2 switched on, so it reads as ten. Every position is worth double the one on its right.

All ones

print(0b1111)

Four ones is 8 plus 4 plus 2 plus 1, which is fifteen. All ones is always one less than the next power.

Step by one

for i in range(8):
    print(bin(i))

Counting up adds one to the row, and the carry rolls left the same way decimal carries.

Read it back

print(int("10110", 2))

Text read as base two gives twenty two. The digits are weights, not counts of things.

The place value doubles

print(0b1, 0b10, 0b100, 0b1000)

Moving a 1 one place left doubles it. That doubling is the whole rule of the system.

Two digits only

print(0b10 == 2)

There is no digit three in binary, so two is written as one followed by zero. The count of digits grows fast.

Compare the systems

print(12, bin(12))

The same number written in decimal and in binary. The value is one thing, the writing is a choice.

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.

← What a bit is
Previous Lesson
What a bit is
Next Lesson
Bytes and the sizes they measure
Bytes and the sizes they measure →