Science and Coding Basics

Binary Numbers and How Computers Store Data

Learn to count in base two and see how computers turn numbers, text and images into bits, with small worked examples you can check by hand.

On this page
  1. Why computers use two digits
  2. Counting in base two
  3. Converting decimal to binary
  4. Bits and bytes
  5. How text becomes bits
  6. How images become bits
  7. The key idea: bits need a rule
  8. Try it yourself

Why computers use two digits

A computer is built from enormous numbers of tiny electronic switches. Each one is reliably in one of two states, which we can call off and on, or 0 and 1. Distinguishing two states is much easier and more dependable than distinguishing ten, so the binary system, which uses only the digits 0 and 1, fits the hardware well.

A single 0 or 1 is called a bit. Everything a computer stores, whether a number, a message, a photograph or a song, is ultimately a long sequence of bits. What differs is the agreed rule for interpreting them.

Counting in base two

You already know a place-value system. In everyday decimal numbers each place is worth ten times the one to its right: ones, tens, hundreds. In the number 345 you have 3 hundreds, 4 tens and 5 ones.

Binary works the same way, except each place is worth twice the one to its right. From the right, the places are worth 1, 2, 4, 8, 16, 32, 64, 128 and so on.

To read a binary number, add up the values of the places that hold a 1.

Example: binary 1101. The places from the right are 1, 2, 4 and 8. The digits from the right are 1, 0, 1, 1. So we add 1 (yes), skip 2 (no), add 4 (yes), add 8 (yes): 1 + 4 + 8 = 13.

Counting from 0 to 7 in binary looks like this:

Decimal Binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111

Notice that three bits give eight different patterns. In general, n bits give 2 to the power n patterns, so 8 bits give 256.

Converting decimal to binary

To convert a decimal number, find the largest place value that fits, mark a 1 there, subtract, and repeat with what is left.

Example: 25. The place values are 16, 8, 4, 2, 1. The largest that fits is 16, leaving 25 - 16 = 9. Next, 8 fits, leaving 1. 4 and 2 do not fit, and 1 does. So the places used are 16, 8 and 1, which gives 11001. Check by adding back: 16 + 8 + 1 = 25.

A handy fact: 11111111 equals 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, one less than 256 because counting starts at 0.

Bits and bytes

Eight bits are grouped into a byte. One byte can hold a value from 0 to 255. Larger amounts are described with prefixes: kilobyte, megabyte, gigabyte. Be aware that these prefixes are used in two ways. In many contexts a kilobyte means 1,000 bytes, while some software uses 1,024 bytes (a power of two) and sometimes calls that a kibibyte. File sizes can therefore differ slightly between programs, which is the kind of thing that matters when you are doing unit conversion.

How text becomes bits

Computers store text by giving each character a number, then storing the number in binary. A long-standing example is ASCII, which assigns numbers to basic English letters, digits and punctuation. In it, the capital letter A is 65. In binary, using 8 bits, that is 01000001, because 64 + 1 = 65. A lower-case a is 97, which is 01100001 (64 + 32 + 1).

So the word “Hi” is two numbers: H is 72 and i is 105. In binary, 72 is 01001000 (64 + 8) and 105 is 01101001 (64 + 32 + 8 + 1). The stored data is those 16 bits in order.

ASCII covers only a small set of characters. Modern systems usually use Unicode, a much larger shared catalog covering many writing systems and symbols, which follows the same principle of a number for each character. Shared rules of this kind are an example of why agreed open standards matter: two programs can only exchange text if they interpret the bits the same way.

How images become bits

A digital image is a grid of small squares called pixels. Each pixel has a color, and a color can be described by numbers.

A common scheme describes each pixel with three values for red, green and blue light, each from 0 to 255, so one byte each. That is 3 bytes, or 24 bits, per pixel. Pure red would be (255, 0, 0); black is (0, 0, 0); white is (255, 255, 255).

A small calculation shows why images are large. An uncompressed image 1,000 pixels wide and 1,000 pixels tall has 1,000,000 pixels. At 3 bytes each, that is 3,000,000 bytes, about 3 megabytes. Formats such as JPEG and PNG use compression to describe the same picture in fewer bits, which is why photo files are usually much smaller. File formats and hosting are a larger topic than this article covers.

Sound is similar: the wave is measured many times per second and each measurement is stored as a number.

The key idea: bits need a rule

The same pattern of bits can mean different things depending on how a program interprets it. The byte 01000001 is the number 65 if read as a number, the letter A if read as ASCII text, or part of a color if read as image data. The bits do not carry their own meaning. The meaning comes from the format, which is why opening a file in the wrong program produces garbage.

Cloud storage and local drives are the same in this respect: both hold bits, and the format is what matters.

Try it yourself

Write the numbers 0 to 15 in binary and check that 15 comes out as 1111. Building a feel for number sense in different bases makes later topics easier to follow.

The complete guide to science and coding basics

Computational Thinking: Solving Problems Like a Programmer
Read the guide

Keep exploring

Guides from other topics

All topics →