IB COMPUTER SCIENCE | A1.2.2 How Binary Stores Data

Lesson objective

Explain how binary encodings represent integers, characters, strings, images, audio and video, and how encoding rules affect storage and retrieval.

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 ruleExample bitsMeaning
Unsigned eight-bit integer 500000101The numerical value five.
ASCII character 5 in a byte00110101A textual digit symbol with code 53.
ASCII character A in a byte01000001The 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.

Code points and their UTF-8 bytes
Text elementCode pointUTF-8 byte bitsBytes

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 representationBits for one pixelInterpretation
Two-bit grayscale palette10The third shade in the defined four-shade palette.
Eight-bit grayscale10000000Brightness value 128 under the chosen 0–255 rule.
24-bit RGB11111111 00000000 00000000R = 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

Sampling and two-bit amplitude encodingEight equally spaced samples of a continuous model waveform are rounded to four amplitude levels. Their two-bit codes are 10, 11, 10, 01, 00, 01, 10, 11.01231011100100011011Time: eight samples across one second | codes shown below
Arbitrary amplitude units. Green = original waveform; pink = quantised samples. A sample code identifies a level, and its time comes from the sample order and rate.
Sample position01234567
Quantised level23210123
Two-bit code1011100100011011

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 dataRules or metadata neededWhat goes wrong without them?
Integer bytesWidth, signed representation and byte order where relevant.A pattern can be interpreted as a different number.
Text bytesCharacter encoding and string or record boundaries.Characters can be misdecoded or data divided incorrectly.
Raster pixelsDimensions, row order, colour depth and palette/component rules.The same bits may produce different shades, colours or layout.
Audio samplesSample rate, bit depth, sample representation and channels.Timing, amplitude or channel arrangement can be wrong.
Video dataFrame 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

MATCH THE ENCODING TERM TO ITS MEANING

Terminology

Terminology

Binary encoding

An agreed mapping between information and bit patterns.

Bit

A binary digit with value 0 or 1.

Byte

A group of eight bits.

Decoder

A process interpreting an encoded representation according to its rules.

Metadata

Information describing how data is organised or should be interpreted.

Unsigned integer

A whole-number interpretation with no negative values.

Signed representation

An agreed convention for representing positive and negative values.

Byte order

The arrangement of bytes within a multi-byte value.

ASCII

A character-code scheme with 128 code values, fitting in seven bits.

Unicode code point

A numerical identifier in the Unicode code space.

UTF-8

A Unicode encoding using one to four bytes per scalar value.

String

A sequence of text elements encoded using an agreed structure.

Terminator

A designated marker used to end a field under a specified format.

Raster image

An image organised as rows and columns of pixels.

Pixel

An individual picture element represented according to a colour or brightness rule.

Palette

A table mapping pixel indices to colours or shades.

Colour depth

The number of bits used for pixel representation under a defined scheme.

RGB

A representation using red, green and blue components.

Sampling

Measuring a signal at discrete times.

Sample rate

The number of samples taken per second per channel.

Bit depth

The number of bits used for each sample or coded value in the stated context.

Quantisation

Mapping measured or calculated values to a finite set of levels.

Frame

One image in a video sequence.

Frame rate

The number of frames presented per second.

Codec

Rules or software for encoding and decoding media data.

Container

A format organising streams such as video, audio and metadata.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. What makes a stored bit pattern meaningful?
2. Which are valid interpretations of 00110101 under the rules given?
3. Which text statements are accurate?
4. What is the UTF-8 payload size for the precomposed string Aé🚀?
5. Which are needed to reconstruct the tiny palette image?
6. How many raw pixel bits are in a 4 × 4 image at two bits per pixel?
7. Which audio statements are correct?
8. One second of mono audio has 8000 samples and sixteen bits per sample. What is its raw payload?
9. Which describe compressed video?
10. Which are accurate cautions?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Explain why bits alone do not uniquely specify the intended data type.

2. Explain the difference between an integer 5 and the ASCII digit character 5.

3. Explain why UTF-8 byte count can differ from code-point count.

4. Explain what is needed to reconstruct a stored raster image.

5. Explain why a uniform image still uses 32 bits in the tiny uncompressed format.

6. Explain how digital audio represents a changing waveform.

7. Explain the separate effects of sample rate and bit depth.

8. Explain how video differs from one still image.

9. Explain why encoding affects retrieval.

10. Calculate and explain the tiny video payload used in the lesson.

EXPLAIN IT | A ROBOT CAMERA AND MICROPHONE

A robot stores a text label, a palette image and audio samples. For each, explain what the binary values represent, how the sequence is organised and what decoding information is required. Distinguish the image’s dimensions from colour depth and the audio’s sample rate from sample bit depth.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for A1.2.2 How Binary Stores Data is coming soon. Complete the matching task and written questions in your exercise book.