Learn
A1.2.2 | HOW BINARY STORES DATA
01 | BITS NEED A RULE BEFORE THEY HAVE A MEANING
A stored sequence such as 00110101 does not announce “I am a number” or “I am text”. Under an unsigned integer rule it represents 53. Under ASCII it represents the character 5. As an eight-bit grayscale pixel it can represent brightness 53 out of 255.
Binary encoding is an agreed way of mapping information to bit patterns. Software needs the encoding, structure and relevant metadata to recover the intended information. The same physical bits can have different meanings under different rules.
In this lesson, follow how integers, characters, strings, images, audio and video become binary data. For each type, ask: what is measured or assigned a code, how are the bits organised, and what must the decoder know?
A decoder is the process that interprets an encoded representation. It does not guess the meaning of every arbitrary bit pattern correctly without context.
02 | WHY TWO STATES ARE USEFUL FOR STORAGE
Electronic circuits can distinguish ranges treated as two logical states. Storage technology provides physical states that can be mapped to 0 and 1: for example, electronic charge states in flash or magnetic recording patterns on an HDD.
These are physical mechanisms interpreted through encoding and control circuitry. A zero is not universally “no electricity anywhere”, and a hard drive does not store tiny printed characters 0 and 1.
Bits can be grouped into larger fields. Eight bits form a byte. A fixed-width n-bit field has 2ⁿ possible patterns, so choosing a width affects both the number of distinguishable values and the space needed.
The storage system also needs ways to locate and organise the bytes. Files, records, addresses and format structures let software retrieve the intended data rather than simply receiving an unexplained stream.
03 | INTEGERS: VALUE, WIDTH AND SIGN CONVENTION
A non-negative integer can be encoded using binary place values. The integer 5 is 101₂, or 00000101 when held in an unsigned eight-bit field. Its value comes from 4 + 1.
Eight unsigned bits represent 0 through 255. A larger value needs more bits or a different representation. Width is therefore part of the rule: a program must know how many bits belong to the integer.
A signed rule changes the interpretation. For example, an eight-bit two’s-complement convention represents values −128 through 127, and 11111111 represents −1 rather than the unsigned value 255. This example shows why the convention matters; it is not a claim that all stored integers use one width or format.
When an integer occupies several bytes, their ordering must also be agreed. Correct bits in an unexpected byte order can decode to a different value. An encoding is more than a list of possible symbols.
04 | TRY INTERPRETING THE SAME BYTE
Enter exactly eight bits. The activity keeps those bits fixed while displaying several possible interpretations. Only the specified rules are being applied; no interpretation is inherently the one the byte “really is”.
The Boolean example deliberately uses a one-byte convention where 00000000 means false and 00000001 means true; other patterns are invalid under that particular rule. Other systems may use different Boolean representations.
05 | CHARACTERS: ASSIGN SYMBOLS NUMERICAL CODES
A character encoding connects textual symbols to numerical codes and byte sequences. ASCII assigns codes to a limited set including letters, digits, punctuation and control characters. Its code values fit in seven bits, although they are commonly held in bytes.
The ASCII code for A is decimal 65, written as 01000001 in a byte. The code for the digit character 5 is decimal 53, written 00110101. That differs from the ordinary unsigned integer value 5, stored as 00000101 in our eight-bit example.
A character displayed as a digit is still text when encoded as a character. A program that wants to calculate with entered text must interpret or convert it according to its rules.
| Information and rule | Example bits | Meaning |
|---|---|---|
| Unsigned eight-bit integer 5 | 00000101 | The numerical value five. |
| ASCII character 5 in a byte | 00110101 | A textual digit symbol with code 53. |
| ASCII character A in a byte | 01000001 | The letter A, code 65. |
06 | UNICODE AND UTF-8: MORE SYMBOLS, VARIABLE BYTE COUNTS
ASCII cannot cover all the writing systems and symbols people use. Unicode assigns code points to a much larger repertoire. A code point is a numerical identifier, commonly written with a U+ hexadecimal label.
An encoding such as UTF-8 maps Unicode scalar values to byte sequences. UTF-8 uses one to four bytes for a code point. ASCII-range values use the same single-byte patterns as ASCII. A precomposed é uses two bytes, and the rocket symbol 🚀 uses four.
Do not say that every Unicode character always uses sixteen bits. Unicode and a particular encoding are different concepts, and the byte count depends on the encoding and code point.
A visible symbol can also involve more than one code point, such as a letter followed by a combining accent. Counting bytes, code points and visible symbols can therefore give different totals. The next activity reports code points and UTF-8 bytes explicitly.
07 | STRINGS: STORE A SEQUENCE AND KNOW ITS BOUNDARIES
A string is a sequence of text elements. In a simple text file using UTF-8, its code points are encoded into an ordered byte sequence. For Cat, the ASCII-range bytes are C = 01000011, a = 01100001 and t = 01110100.
Software must know the encoding and where the string ends. A format can specify a byte length, use a terminator or define another structure. It cannot assume that every possible string ends after three bytes.
Decoding bytes with the wrong encoding can produce incorrect characters or an error. The original byte count also does not tell you the number of human-visible characters without knowing the representation.
Try the examples or enter up to twelve Unicode code points. The activity shows a UTF-8 payload only; it does not count a file header, terminator, programming-language object overhead or storage-allocation overhead.
| Text element | Code point | UTF-8 byte bits | Bytes |
|---|
08 | IMAGES: PIXEL VALUES AND A DEFINED COLOUR RULE
A raster image is organised as pixels in rows and columns. Each pixel’s bits identify a brightness or colour according to an agreed rule. The decoder also needs dimensions, pixel order and information about the colour representation.
For an eight-bit grayscale image, a common rule uses values 0–255 for brightness, with 0 black and 255 white. With two bits per pixel, only four distinct patterns are available. A defined palette could map 00, 01, 10 and 11 to four chosen shades.
An RGB image represents red, green and blue components. A common 24-bit RGB representation uses eight bits per component. The pattern 11111111 00000000 00000000 represents maximum red and zero green and blue under that rule.
More bits per pixel allow more distinct coded values, but they do not automatically add more spatial detail. Pixel dimensions and colour depth describe different properties.
| Example representation | Bits for one pixel | Interpretation |
|---|---|---|
| Two-bit grayscale palette | 10 | The third shade in the defined four-shade palette. |
| Eight-bit grayscale | 10000000 | Brightness value 128 under the chosen 0–255 rule. |
| 24-bit RGB | 11111111 00000000 00000000 | R = 255, G = 0, B = 0 under this component order. |
09 | BUILD A TINY IMAGE AND READ ITS BITS
This image has four columns, four rows and two bits per pixel. Its palette is 00 black, 01 dark gray, 10 light gray and 11 white. Click a pixel to move through those four values.
Pixels are stored left to right across the first row, then across each following row. That row-major order, the dimensions and the palette are essential decoding rules. The payload is always 16 × 2 = 32 bits, even when all pixels have the same value.
You can also replace the payload with exactly 32 bits and decode it. This is an uncompressed teaching format. It does not pretend to be a complete PNG or other image file.
10 | AUDIO: MEASURE AMPLITUDE AT REGULAR TIMES
A sound waveform varies continuously. A digital recording can sample its amplitude at regular times, then quantise each measured amplitude to one of a finite set of levels. Each level is represented by a binary sample value.
Sample rate is the number of samples taken per second per channel. Bit depth is the number of bits used for a sample, determining the number of available code patterns. For example, two bits allow four patterns; sixteen bits allow 65,536.
The decoder needs the sample rate, sample representation and channel arrangement to interpret the values and their timing. An audio output system reconstructs a signal from that information. The bits do not literally store sound waves inside the file.
The diagram below uses only eight samples in one second and four illustrative amplitude levels. This tiny teaching example is not suitable-quality real-world audio. The pink points show rounded amplitude levels, while the green curve is the original model waveform.
11 | FOLLOW THE AUDIO CODES AND THEIR STORAGE
| Sample position | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Quantised level | 2 | 3 | 2 | 1 | 0 | 1 | 2 | 3 |
| Two-bit code | 10 | 11 | 10 | 01 | 00 | 01 | 10 | 11 |
The eight two-bit samples use sixteen payload bits in this one-channel example. No header, timing field or other metadata is included in that total.
For fixed-width, uncompressed sample data, the payload calculation is sample rate × bit depth × duration × channels. A one-second recording at 8,000 samples per second, sixteen bits per sample and one channel uses 128,000 bits, or 16,000 bytes, before overhead.
Sample rate affects temporal sampling; bit depth affects amplitude precision. Increasing either generally increases the raw data amount, but they address different properties. Compressed audio formats encode that information differently, so this raw-payload formula is not an exact compressed-file-size formula.
12 | VIDEO: IMAGES OVER TIME, OFTEN WITH AUDIO
Uncompressed video can be represented as a sequence of image frames with timing information. Each frame contains pixel data. Frame rate describes how many frames are displayed per second, while frame dimensions and pixel representation determine the raw data per frame.
A tiny uncompressed 4 × 4 video using two bits per pixel has 32 pixel-data bits per frame. At ten frames per second for two seconds, twenty frames need 640 bits, or eighty bytes, before timing metadata, headers or audio.
Real video formats commonly use compression. Some coded frames carry information that can be reconstructed without a prediction from other frames; others use predictions and encoded differences based on reference frames. The decoder needs the codec rules and the relevant reference information, not just an assumption that every frame is a full separate bitmap.
A container can hold video, audio and metadata needed to keep playback coordinated. Seeking to a point in compressed video may require decoding from a suitable earlier point and reconstructing dependent frames. That affects retrieval as well as file size.
13 | WHAT THE DECODER NEEDS TO RETRIEVE THE RIGHT INFORMATION
| Stored data | Rules or metadata needed | What goes wrong without them? |
|---|---|---|
| Integer bytes | Width, signed representation and byte order where relevant. | A pattern can be interpreted as a different number. |
| Text bytes | Character encoding and string or record boundaries. | Characters can be misdecoded or data divided incorrectly. |
| Raster pixels | Dimensions, row order, colour depth and palette/component rules. | The same bits may produce different shades, colours or layout. |
| Audio samples | Sample rate, bit depth, sample representation and channels. | Timing, amplitude or channel arrangement can be wrong. |
| Video data | Frame timing, dimensions, codec and container/reference information. | The player may be unable to reconstruct or schedule frames correctly. |
Files and formats supply structure. A file extension can help software choose a handler, but changing a name from .txt to .png does not transform the bytes into a valid image. The actual data must follow the expected format.
When retrieving data, software locates and reads the required bytes, then decodes them using those rules. Compression may introduce additional decoding work or dependencies; fixed-size records and known offsets can make some lookups more direct. Different formats have different retrieval costs.
14 | EXPLAIN THE CONNECTION BETWEEN THE DATA AND THE BITS
For explain, connect the information, the code rule and its consequence. For example: “Each pixel uses a two-bit palette index, so only four palette values are available. The decoder needs the palette and pixel order to rebuild the intended image.”
For text: “A character is assigned a code point and encoded as bytes. Because UTF-8 has variable-length encodings, the number of bytes need not equal the number of code points.”
For audio: “Amplitude samples are taken at regular times and mapped to finite levels. Their binary codes and the sample rate allow the sequence to be interpreted as timed signal data.”
Do not answer only “everything is stored as zeros and ones”. Explain what each pattern stands for, how it is organised and which rule lets the computer retrieve the intended information.
Further reading: Unicode encoding forms · Video encoding and frame dependencies