Every photo, song, message and web page on your computer is, deep down, a long list of numbers. And every one of those numbers is written with just two symbols: 0 and 1. That sounds impossibly limited, but you already know the trick that makes it work. You use it every time you read a number like 407. This page shows you that trick, lets you flip some switches yourself, and ends with how the letter A turns into a number a computer can store.

The trick you already know: place value

We count in decimal, also called base 10: we have ten digits, 0 to 9. When we run out of digits (after 9), we don't invent a new symbol. We put a 1 in the next spot to the left and start again from 0. That's why 10 comes after 9.

Each spot in a number is called a place, and each place is worth ten times the one to its right. Take 407:

4
hundreds
4 × 100
0
tens
0 × 10
7
ones
7 × 1

400 + 0 + 7 = 407. The digit tells you how many, and the place tells you of what. You do this sum so quickly you don't notice you're doing it.

Same trick, only two digits

Computers are built from billions of tiny electronic switches called transistors. A switch is either off or on, so it's natural to use just two digits: 0 for off, 1 for on. Counting with two digits is called binary, or base 2.

The place-value rule stays exactly the same, except that each place is worth two times the one to its right instead of ten. So the places, from right to left, are worth 1, 2, 4, 8, 16, 32, 64 and 128. Each binary digit says "include this place value" (1) or "skip it" (0), and the number is the total of everything included.

Try it. Tap the switches below. Each one is a single binary digit with its place value written above it.

Flip the switches · one byte

0all switches off = 0
Tap any switch to turn it on. Then press +1 a few times and watch the switches on the right flip like an odometer.

Press +1 from zero and you'll see the binary sequence: 1, 10, 11, 100, 101, 110, 111, 1000… It's the same thing your car's odometer does with 9 → 10, except binary "rolls over" at every 1. When a place is already 1 and you add 1, it becomes 0 and carries a 1 to the left, just like 9 + 1 in decimal.

Reading binary out loud

To turn binary into a normal number, add up the place values under each 1. 00101010 has 1s under 32, 8 and 2, so it's 32 + 8 + 2 = 42.

Going the other way is a "biggest piece first" game. To write 42 in binary: the biggest place value that fits in 42 is 32. Switch it on, 10 is left. 16 doesn't fit in 10, so skip it. 8 fits: on, 2 left. 4 doesn't fit. 2 fits: on, 0 left. Done. Use Give me a target in the panel above to practise this.

Programmers often write a little prefix so you can tell which base a number is in: 0b101 means binary 101 (that's 5), while plain 101 means a hundred and one. Without a hint, "10" could mean ten or two.

Bits and bytes

One binary digit is called a bit (short for binary digit). A single bit can only answer a yes/no question: on or off, true or false.

Bits are almost always handled in groups of eight. A group of 8 bits is a byte. The switch panel above is exactly one byte. File sizes are measured in bytes: a kilobyte is about a thousand bytes, a megabyte about a million, a gigabyte about a billion.

Why one byte stops at 255

Turn all eight switches on. The total is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255. That's the biggest number one byte can hold, because there's nowhere left to carry to.

Counting zero, that's 256 different values (0 to 255). Here's a neat way to see why: each bit has 2 choices, and the choices multiply. 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 = 28 = 256. The biggest number is one less than that, because one of the 256 patterns is spent on zero.

Now try pressing +1 when every switch is on. All eight bits roll over to 0 and the carry falls off the left edge, because there's no ninth switch to catch it. This is called overflow. Real programs mostly use bigger groups of bytes to avoid it, but it's a classic source of bugs.

You meet the 0–255 range all the time: colours on screens are usually made of red, green and blue amounts from 0 to 255, one byte each. Drag the slider to see how fast the range grows as you add bits.

How many bits?

8 bits
Different values256
Biggest number255

Every extra bit doubles the number of values. That's why 32 bits already gives over four billion and 64 bits gives a number with twenty digits.

How a letter becomes a number

A computer can only store numbers, so text has to be turned into numbers too. The fix is simple: everyone agrees on a table that gives each character a number. This kind of table is called a character encoding.

ASCII: the original table

The classic one is ASCII (pronounced "ASK-ee"), from the 1960s. It numbers 128 characters: English letters, digits, punctuation, the space, plus some invisible control codes like "new line". A few entries:

CharacterNumberIn binary
A6501000001
B6601000010
a9701100001
0 (the digit)4800110000
(space)3200100000

Two things worth noticing. First, the digit "0" is stored as 48, not 0: the character you see on screen and the number it stands for are different things. Second, compare A (01000001) with a (01100001). They differ in exactly one bit, the one worth 32. Every lowercase ASCII letter is its uppercase partner plus 32. The designers set it up that way on purpose.

UTF-8: room for every language (and emoji)

128 characters is nowhere near enough for the world's languages. Today there's one giant list called Unicode that gives a number (a code point) to over a hundred thousand characters, from é and ж to 中 and 😀. That's far more than one byte can count to.

The usual way to store Unicode is UTF-8, and nearly every web page uses it. Its clever rule: the first 128 characters are stored exactly as in ASCII, one byte each. Everything else takes 2, 3 or 4 bytes. So plain English text costs one byte per letter, and older ASCII files are automatically valid UTF-8.

Type anything below and see the actual bytes a computer would store.

Type a word, see its bytes

Characters with an orange border need more than one byte. In binary view, look at the orange bits at the start of each byte. They're UTF-8's signposts: a byte starting with 0 is a complete one-byte character, 110 means "I start a 2-byte character", 1110 starts a 3-byte one, 11110 starts a 4-byte one, and 10 means "I'm a continuation byte". That's how a computer reading the bytes knows where each character begins.

The hex view uses hexadecimal (base 16), which you'll see a lot. It uses digits 0–9 and then A–F for 10–15, so any byte fits in exactly two hex digits: 255 is FF. Programmers like it simply because it's shorter than eight 0s and 1s.

This is why "how long is this text?" has two honest answers. héllo is 5 characters but 6 bytes, because é takes two. When a website limits a field to "N bytes", emoji eat into that limit four times faster than plain letters.

Check yourself

Work each one out first, then tap an answer.

What is the binary number 00001010 in decimal?

The 1s sit under the place values 8 and 2. 8 + 2 = 10. The leading zeros add nothing, just like 007 is still 7.

What's the biggest number one byte can hold?

A byte has 28 = 256 different patterns, but one of them is 0, so the values run from 0 to 255.

How many bytes does the word cat take in UTF-8?

c, a and t are all plain ASCII letters, and UTF-8 stores those in one byte each. (24 is the number of bits.)

In ASCII, B is 66. What number is b?

Each lowercase letter is its uppercase partner plus 32: 66 + 32 = 98. In binary, that's just switching on the 32 bit.

The short version

Next time you see a "255" in a colour picker, you'll know it's not a random limit. It's simply eight switches, all on.