Basic

Bytes and the sizes they measure

A byte is eight bits, and it is the unit files and memory are counted in. Sizes grow in steps of a thousand-ish, which is why file sizes look the way they do.

What you will see

1024

About 2 min of reading

Try It

Measure sizes in bytes.

1

Show the wrap at the top of a byte.

print(256 % 256) print(256 // 256)

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

What it prints
0 1
2

Print what two bytes hold.

print(2 ** 16)

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

What it prints
65536
3

Count the bytes of a word.

print(len("owlww".encode("utf-8")))

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

What it prints
5
4

Print the thousand step.

print(2 ** 10)

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

What it prints
1024

Data Flow

A byte is eight bits and the unit of storage, and sizes step by 1024 because that is a power of two.
1 one byte holds 256 values
↓
2 two bytes hold 65536
↓
3 a kilobyte is 1024 bytes
↓
4 a byte is the smallest addressable piece
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(256 % 256) 0
print(256 // 256) 1
Why this works
Byte is the addressable unit

A machine points at bytes, not bits. A bit is inside a byte and can only be reached by masking, which is what later lessons do.

The step is a power of two

Sizes are powers of two because storage is binary. That is why a kilobyte is 1024 and not a round thousand.

Examples

One byte holds 256

print(256 % 256)
print(256 // 256)

The two hundred fifty seventh value does not fit in one byte, so it carries into the next. That is the wrap at the top.

Two bytes hold 65536

print(2 ** 16)

Two bytes hold sixty five thousand five hundred thirty six values. Each byte added doubles the room by two hundred fifty six.

Kilobytes and the step

print(1024, 1024 * 1024)

A kilobyte is 1024 bytes, a megabyte is 1024 of those. The step is 1024 because it is a power of two.

Count a file

print(len("owlww".encode("utf-8")))

Five plain letters are five bytes. One accented letter takes two, which is why sizes are not letter counts.

Bytes are addressable

b = bytearray(4)
print(len(b))

Memory is counted in bytes, not bits. A byte is the smallest piece a machine can point at on its own.

A big row

print(len(bytearray(1024 * 1024)))

A megabyte is a million-ish bytes. Sizes that big are counted in steps, never in single bytes.

Why 1024

print(2 ** 10)

One thousand twenty four is the power of two nearest a thousand. Binary counting makes that the natural step.

Sizes in the wild

print(3 * 1024 * 1024)

Three megabytes is three million-ish bytes. A download bar is that count divided into steps.

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.

← Counting in binary
Previous Lesson
Counting in binary
Next Lesson
Hexadecimal
Hexadecimal →