Data Basics

How Binary Represents Numbers, Text and Images

By BinaryStem Editorial Team · · 5 min read

Inside every computer, everything is reduced to two symbols: 0 and 1. Photos, documents, spreadsheets and videos are all long strings of these two digits. That sounds impossible until you see the trick. The meaning does not live in the digits alone. It comes from an agreed rule, called an encoding, that says how to read them.

Once you know a few such rules, the idea becomes clear and surprisingly simple.

What a bit and a byte are

A bit is a single binary digit, either 0 or 1. It maps neatly onto physical hardware, where a tiny circuit can be off or on, or a storage cell can hold one of two states.

A byte is a group of eight bits. One byte can form 256 different patterns, from 00000000 to 11111111. Larger quantities are counted in kilobytes, megabytes and gigabytes, each a bigger collection of bytes.

Representing whole numbers

We normally count in base ten, where each position is worth ten times the one to its right. Binary works in base two, where each position is worth twice the one to its right.

For an eight-bit number, the positions from right to left are worth 1, 2, 4, 8, 16, 32, 64 and 128. To read a binary number, add up the values of the positions holding a 1.

00101101
 = 32 + 8 + 4 + 1
 = 45

To convert the other way, find which position values add up to your number. With eight bits you can store values from 0 to 255. Using more bits stretches that range: sixteen bits reach 65,535, and thirty-two bits reach over four billion.

Negative numbers

To store negative values, computers commonly use two's complement. The leftmost bit signals the sign, and the arrangement is chosen so that ordinary binary addition still works for both positive and negative numbers. In an eight-bit version, values range from -128 to 127. The details are clever, but the key point is that a sign is just another agreed pattern in the bits.

Representing decimals

Fractions need a different approach. Most computers use floating-point representation, which resembles scientific notation. A number is stored as a sign, a set of bits for the significant digits and a set of bits for an exponent that shifts the decimal point.

This explains a familiar quirk. Some simple decimal values, such as 0.1, cannot be written exactly in binary, just as one third cannot be written exactly in decimal. The stored value is a very close approximation, which is why a calculation like 0.1 plus 0.2 can display a tiny error in many programming languages.

print(0.1 + 0.2)

Programs that need exact decimal results, such as accounting software, use special decimal types or store whole units to avoid this.

Representing text

Text is stored by giving each character a number, then storing that number in binary. A table that links characters to numbers is a character encoding.

ASCII is an early standard using seven bits to cover English letters, digits, punctuation and a few control codes. In ASCII, the capital letter A is the number 65, which is 01000001 in binary. A lowercase a is 97.

ASCII could not handle the world's writing systems, so Unicode was created. It assigns a unique number, called a code point, to characters across languages, symbols and emoji. UTF-8 is the most common way to store Unicode. It uses one byte for basic English characters, matching ASCII, and two to four bytes for others.

So the word "Hi" is just two numbers, 72 and 105, stored as two bytes. The reader's software knows to interpret them as letters because the file is declared or assumed to be text.

Representing images

An image is a grid of tiny dots called pixels. Each pixel's color is stored as numbers.

In a common color scheme, each pixel has three values for red, green and blue light. Using one byte per value, each can range from 0 to 255. A pixel with red 255, green 0 and blue 0 is pure red. Mixing the three produces millions of colors.

For a grayscale image, a single byte per pixel is enough, with 0 as black and 255 as white. A tiny black-and-white image can even use one bit per pixel.

The file size follows directly from the arithmetic. An uncompressed image with a certain width and height needs width times height times three bytes for color. Real image formats usually apply compression to shrink this. Some compression methods keep every detail, called lossless, and others discard fine detail the eye is unlikely to miss, called lossy.

Everything else follows the same pattern

Sound is stored by sampling a waveform many times per second and recording each measurement as a number. Video is a rapid sequence of images with synchronized audio. Programs themselves are sequences of numeric instructions the processor understands.

In every case, the recipe is the same: choose a rule for mapping real things to numbers, then write the numbers in binary.

Why context matters

The same bits can mean different things depending on how they are read. The byte 01000001 could be the number 65, the letter A, or part of a pixel color. Software relies on file types, headers and format definitions to know which rule applies. That is why opening an image in a text editor produces gibberish: the program is applying the wrong rule.

Frequently Asked Questions

Why do computers use binary instead of decimal?

Binary matches the physical hardware. Electronic components can reliably distinguish two states, such as high and low voltage, much more easily than ten. Two-state circuits are simple, fast and less prone to error.

How many different values can n bits store?

Two to the power of n. Eight bits give 256 patterns, sixteen give 65,536, and each additional bit doubles the count. Whether those patterns mean numbers, letters or colors depends on the encoding.

What is the difference between a byte and a character?

A byte is eight bits. A character is a unit of text. In ASCII, one character fits in one byte, but in UTF-8 a single character may take one to four bytes, depending on which character it is.

Related reading