Bits and bytes

What a bit physically is, why computers use only two levels, and how eight bits become a byte. No prior knowledge needed.

On this page
  1. 01What a bit physically is
  2. 02Why only two levels
  3. 03From one bit to many patterns
  4. 04The byte
  5. 05A pattern has no meaning by itself
  6. Recap
  7. Try it

A computer is a machine that stores and moves electrical states. That is the whole job.

Your photos, your code, the operating system itself: all of it is patterns of those states, held in wires and tiny cells and moved around very fast. This lesson covers what one unit of that state is, how units are grouped, and what a group means. It assumes nothing.

What is a bit? Why only two levels? How do bits turn into something useful?

What a bit physically is

A bit is one place that can be in one of two states. In hardware, a state is a physical thing you can measure.

  • In a CPU wire: a voltage that is low or high.
  • In RAM: a tiny cell. In common DRAM, a capacitor holds charge or doesn't, and a transistor guards it.
  • On an SSD: electric charge trapped in a cell, or not.
  • On a hard disk: a tiny magnetic region pointing one way or the other.
  • On a network cable or fibre: a pulse of voltage or light, present or absent.

The physics differs in every case. The abstraction is the same: two states. We label them 0 and 1, but those are only names. Nothing in the hardware is a "1".

Why only two levels

Why not ten levels, one per decimal digit?

Because real signals are never clean. A voltage drifts with temperature, with wire length, with interference from the wire next to it. If you ask a circuit "what voltage is this?", the answer is never exact.

So circuits do not measure the voltage. They compare it against a threshold. Above it, the circuit treats the signal as 1. Below it, as 0. The two zones are kept far apart, with a gap between them that signals must cross quickly.

reads as 1 undefined reads as 0 high low noise moves the line, but never across the gap
Noise moves the line around inside a zone. It never has to cross the gap, so the reading stays correct.

Now imagine ten levels. The same voltage range gets cut into ten narrow zones, and a bit of noise pushes a 4 into a 5. Fewer levels means wider zones, which means fewer errors.

That is the reason, and it is an engineering one. Two levels are the easiest thing to read reliably at billions of switches per second.

From one bit to many patterns

One bit can only distinguish two things. To distinguish more, use more bits.

Here is the rule for how many patterns you get. Take all the patterns for n bits, and add one more bit. Each old pattern can now end in 0 or in 1, so every old pattern becomes two. The count doubles.

1 bit012 patterns 2 bits000110114 patterns 3 bits0000010100111001011101118 patterns 8 bits00000000 … 11111111256 patterns
Each extra bit doubles the number of patterns.

So n bits give 2ⁿ patterns.

It grows fast: 10 bits give 1,024 patterns, 16 bits give 65,536, and 32 bits give 4,294,967,296.

A pattern is only a distinct label. It has no meaning yet. We will come back to that.

The byte

Hardware does not handle bits one at a time. It moves them in groups, and the standard group is 8 bits, called a byte.

0 1 0 0 1 0 0 0 one byte = eight bits
A byte: eight bits side by side. This one is 0100 1000.

Eight bits give 2⁸ = 256 patterns.

Eight was not the only choice. Older machines used other sizes. Eight became the standard, and today every common system uses it.

The practical reason it matters: memory is addressed in bytes. Think of RAM as one long numbered array. Address 0 holds one byte, address 1 holds the next, and so on. When a program reads memory, it says "give me the byte at address 100". You cannot ask for a single bit by address. The byte is the smallest unit you can point at.

Sizes follow from that:

  • 1 KB is about a thousand bytes. A short text file.
  • 1 MB is about a million bytes. A compressed photo.
  • 1 GB is about a billion bytes. A film.

A pattern has no meaning by itself

Take one byte: 0100 1000. What is it?

It could be the number 72. It could be the letter H. It could be the brightness of one pixel, 72 out of 255.

0100 1000 0x48 the number 72 the letter H a dim pixel (72 of 255) the reader decides
The same eight bits. The meaning depends on who reads them.

Nothing in the byte says which. The meaning comes from an agreement between the program that wrote the byte and the program that reads it.

This is a real, common source of bugs. A file saved as UTF-8 contains the two bytes c3 a9 for the letter é. Open it with a program that assumes a different text format, and you see é instead. The bytes were fine. The reader used the wrong agreement.

A byte is a pattern. What it means is decided by whoever reads it.

Recap

  • A bit is one two-state place: a voltage, a charge, a magnetic direction, a pulse.
  • Circuits use two levels because two wide zones survive noise.
  • n bits give 2ⁿ patterns. Each extra bit doubles the count.
  • A byte is 8 bits, giving 256 patterns. It is the smallest unit memory can address.
  • A pattern has no built-in meaning. The reader supplies it.

Try it

Make a file with one letter and look at its bits.

printf 'A' | xxd -b

You should see 01000001. Write down which positions are 1.

Now two letters:

printf 'Hi' | xxd -b

Two bytes, so two groups of eight. The next lesson shows how to read each group as a number.