Augment Education
  • Blog
Log inStart free
augment

Augment Education

IB Diploma revision built around the syllabus.

contact@augment-education.com

Product

  • Features
  • Question bank
  • Quick fire
  • Worksheets
  • Flashcards
  • Notes
  • Subjects
  • What's new

Learn

  • IB guide
  • Booklets, explained
  • Blog
  • Free tools
  • Grade calculator
  • Subject checker

Company

  • About
  • Help
  • For parents
  • For teachers
  • Student stories
  • Comparisons

Augment Education is developed independently of the International Baccalaureate Organization and is not endorsed by it. “IB” and “International Baccalaureate” are its trademarks.

© 2026 Augment Education. All rights reserved.

PrivacyTerms
  1. Subjects
  2. Computer Science
  3. A1 Computer fundamentals
  4. A1.2 Data representation and computer logic

A1.2 Data representation and computer logic

Convert integers between binary, denary and hexadecimal, explain how text, images, sound and video are stored as bits, and build, analyse and simplify logic circuits with truth tables, Boolean algebra and Karnaugh maps.

  • IB Computer Science
  • Part of A1 Computer fundamentals
  • SL and HL

Key terms introduced here

Each is in the Computer Science glossary, with every other term in the subject.

Absorption law

The rule that a term containing a variable is redundant when it is combined with that variable on its own: A+A⋅B=AA + A \cdot B = AA+A⋅B=A and A⋅(A+B)=AA \cdot (A + B) = AA⋅(A+B)=A.

Analogue signal

A signal that varies continuously, so it can take any value within a range at any moment in time.

Analogue-to-digital converter (ADC)

A circuit that measures an analogue signal at regular intervals and outputs each measurement as a binary number.

AND gate

A logic gate whose output is 1 only when all of its inputs are 1.

ASCII

The American Standard Code for Information Interchange, a 7-bit character set of 128 codes covering the English alphabet, digits, punctuation and control characters.

Associative law

The rule that the grouping of three or more operands joined by the same operator does not change the result: (A⋅B)⋅C=A⋅(B⋅C)(A \cdot B) \cdot C = A \cdot (B \cdot C)(A⋅B)⋅C=A⋅(B⋅C) and (A+B)+C=A+(B+C)(A + B) + C = A + (B + C)(A+B)+C=A+(B+C).

Binary

The base-2 number system, which uses only the digits 0 and 1 and column values that are powers of 2 (1, 2, 4, 8, 16, …).

Binary prefix

A unit prefix based on powers of 1024, as in kibibyte (KiB, 1024 bytes), mebibyte (MiB, 102421024^{2}10242 bytes) and gibibyte (GiB, 102431024^{3}10243 bytes).

Bit

A binary digit, the smallest unit of data, which has one of two values, 0 or 1.

Bit depth

The number of bits used to store each sample of a sound, which sets how many different amplitude levels (2n2^{n}2n for nnn bits) can be recorded.

Bitmap image

An image stored as a grid of pixels, with a binary value recording the colour of each pixel.

Boolean algebra

The algebra of the two values true (1) and false (0), with operations such as AND, OR and NOT and laws for rewriting and simplifying expressions.

Boolean expression

An expression built from Boolean variables, the constants 0 and 1, and Boolean operators, whose value is either true (1) or false (0).

Boolean operator

An operation that combines or changes Boolean values: AND, OR, NOT, NAND, NOR, XOR or XNOR.

Byte

A group of 8 bits, the standard unit for measuring storage and for addressing memory.

Character code

The number that a character set assigns to one character, stored in binary to represent that character.

Character set

An agreed list of characters in which each character is given a unique number, so that text can be stored and exchanged as binary.

Colour depth

The number of bits used to store the colour of each pixel; a colour depth of nnn bits allows 2n2^{n}2n different colours.

Commutative law

The rule that the order of the operands of AND or OR does not change the result: A⋅B=B⋅AA \cdot B = B \cdot AA⋅B=B⋅A and A+B=B+AA + B = B + AA+B=B+A.

Complement law

The rule that ANDing a variable with its inverse gives 0, and ORing a variable with its inverse gives 1: A⋅A‾=0A \cdot \overline{A} = 0A⋅A=0 and A+A‾=1A + \overline{A} = 1A+A=1.

De Morgan's laws

The two rules A⋅B‾=A‾+B‾\overline{A \cdot B} = \overline{A} + \overline{B}A⋅B=A+B and A+B‾=A‾⋅B‾\overline{A + B} = \overline{A} \cdot \overline{B}A+B​=A⋅B, which state that the inverse of an AND is the OR of the inverses, and the inverse of an OR is the AND of the inverses.

Decimal prefix

A unit prefix based on powers of 1000, as in kilobyte (kB, 1000 bytes), megabyte (MB, 100021000^{2}10002 bytes), gigabyte (GB, 100031000^{3}10003 bytes) and terabyte (TB, 100041000^{4}10004 bytes).

Denary

The base-10 number system, which uses the ten digits 0 to 9 and column values that are powers of 10.

Digital signal

A signal that can take only a limited set of discrete values, such as the binary values 0 and 1, at discrete moments in time.

Digital-to-analogue converter (DAC)

A circuit that turns a sequence of binary numbers into a continuously varying analogue signal.

Distributive law

The rule that an operator applied to a bracket can be applied to each term inside it: A⋅(B+C)=A⋅B+A⋅CA \cdot (B + C) = A \cdot B + A \cdot CA⋅(B+C)=A⋅B+A⋅C and A+B⋅C=(A+B)⋅(A+C)A + B \cdot C = (A + B) \cdot (A + C)A+B⋅C=(A+B)⋅(A+C).

Double negation law

The rule that inverting a variable twice gives the original variable: A‾‾=A\overline{\overline{A}} = AA=A.

Frame rate

The number of frames (still images) shown per second of video, measured in frames per second (fps).

Gray code

An ordering of binary values in which each value differs from the next in exactly one bit.

Half adder

A logic circuit that adds two single bits, producing a sum bit (A XOR B) and a carry bit (A AND B).

Hexadecimal

The base-16 number system, which uses sixteen digits, 0 to 9 and A to F (A = 10, B = 11, C = 12, D = 13, E = 14, F = 15), and column values that are powers of 16.

Idempotent law

The rule that ANDing or ORing a variable with itself gives the same variable: A⋅A=AA \cdot A = AA⋅A=A and A+A=AA + A = AA+A=A.

Identity law

The rule that ANDing a variable with 1, or ORing it with 0, leaves it unchanged: A⋅1=AA \cdot 1 = AA⋅1=A and A+0=AA + 0 = AA+0=A.

Image resolution

The number of pixels in an image, usually given as width × height.

Karnaugh map

A grid form of a truth table, with rows and columns labelled in Gray-code order so that adjacent cells differ in one input, used to simplify Boolean expressions by grouping adjacent 1s.

Logic diagram

A diagram of a logic circuit that uses standard gate symbols connected by lines (wires) to show how the inputs are processed to produce the outputs.

Logic gate

An electronic circuit that takes one or more binary inputs and produces a single binary output determined by a Boolean operation.

Metadata

Data that describes other data, recording the properties needed to interpret, organise or find it.

Most significant bit (MSB)

The leftmost bit of a binary number, which has the largest place value; the rightmost bit, with place value 1, is the least significant bit (LSB).

NAND gate

A logic gate whose output is 0 only when all of its inputs are 1, equivalent to an AND gate followed by a NOT gate.

Nibble

A group of 4 bits, half a byte, which can represent 16 values and is written as one hexadecimal digit.

NOR gate

A logic gate whose output is 1 only when all of its inputs are 0, equivalent to an OR gate followed by a NOT gate.

NOT gate

A logic gate with one input whose output is the opposite of the input, 1 for an input of 0 and 0 for an input of 1.

Null law

The rule that ANDing a variable with 0 always gives 0, and ORing it with 1 always gives 1: A⋅0=0A \cdot 0 = 0A⋅0=0 and A+1=1A + 1 = 1A+1=1.

OR gate

A logic gate whose output is 1 when at least one of its inputs is 1.

Overflow

The error that occurs when the result of a calculation is too large to be represented in the fixed number of bits available.

Pixel

The smallest single-coloured element of a bitmap image or display; its name is short for "picture element".

Place value

The value of a digit's position in a number, equal to the base raised to the power of the position, counting from 0 at the right.

Sampling

Measuring the amplitude of an analogue signal at regular, equally spaced moments and storing each measurement, called a sample, as a binary number.

Sampling rate

The number of samples taken per second, measured in hertz (Hz).

Sum of products

A form of Boolean expression made of AND terms (products) joined by OR (a sum).

Truth table

A table that lists every possible combination of input values of a logic circuit or Boolean expression, with the output for each combination.

Two's complement

A way of storing signed integers in which the most significant bit has a negative place value, so an nnn-bit number holds values from −2n−1-2^{n-1}−2n−1 to 2n−1−12^{n-1} - 12n−1−1.

Unicode

A universal character set that gives every character of the world's writing systems, together with symbols and emoji, its own number, called a code point.

Universal gate

A logic gate from which any other logic gate, and therefore any logic circuit, can be built; NAND and NOR are the two universal gates.

Unsigned integer

A whole number stored in binary without a sign, so it can only be zero or positive; with nnn bits it can hold values from 0 to 2n−12^{n} - 12n−1.

UTF-8

A variable-length encoding of Unicode that stores each code point in one, two, three or four bytes, using a single byte for the 128 ASCII characters.

Vector graphic

An image stored as a list of shapes (lines, curves, polygons and text) described by coordinates and properties such as colour and line width, which the computer draws when the image is displayed.

XNOR gate

A logic gate whose output is 1 when its two inputs are equal (both 0 or both 1), equivalent to an XOR gate followed by a NOT gate.

XOR gate

A logic gate whose output is 1 when exactly one of its two inputs is 1, that is, when the inputs are different.

Notes on A1.2 Data representation and computer logic

79 min readLast reviewed 14 Oct 2026

Learning objectives

  • Describe how integers are represented in binary and hexadecimal, including the range of values a fixed number of bits can hold.

  • Calculate the denary, binary and hexadecimal forms of an integer from any one of them, showing the working.

  • Explain how binary is used to store integers, characters and strings, and the effect of the encoding on storing and retrieving data.

  • Explain how images, audio and video are stored in binary, and calculate the storage they need.

  • Describe the purpose and use of logic gates, the seven Boolean operators AND, OR, NOT, NAND, NOR, XOR and XNOR, and their applications in computer systems.

  • Construct and analyse truth tables for logic circuits, Boolean expressions and problems described in words.

  • Construct simplified Boolean expressions using Boolean algebra and Karnaugh maps.

  • Construct logic diagrams from Boolean expressions and problem descriptions, and simplify them.

Introduction

Every value a computer handles, from a bank balance to a photograph or a voice message, is stored as a pattern of two states, written 0 and 1. The same physical signals that store those patterns also process them: tiny electronic switches combine 0s and 1s according to the rules of logic, and millions of these combinations make up the arithmetic and control circuits of a processor. Understanding both halves explains why a photo takes a certain number of megabytes, why an old system can fail when a counter runs out of bits, and how a processor turns a condition in a program into a decision made by hardware.

The subtopic has two parts. Data representation covers number systems (binary, denary and hexadecimal) and the way integers, text, images, sound and video are encoded as bits. Computer logic covers logic gates, truth tables, Boolean algebra, Karnaugh maps and logic diagrams, the tools for designing and simplifying the circuits that process those bits.

Scope: The IB course covers unsigned integers in binary and hexadecimal and conversion between binary, hexadecimal and decimal; how integers, characters, strings, images, audio and video are stored in binary; the seven logic gates and their uses; truth tables; Karnaugh maps and Boolean algebra for simplification; and logic diagrams. Negative numbers (two's complement), floating point and octal are not required: two's complement appears only as a labelled extension. Transistor-level electronics, sequential circuits and don't-care conditions in Karnaugh maps are not required.

YouTube
Crash Course Computer Science, “Representing Numbers and Letters with Binary”: how place value works in base 2, how many values a given number of bits can hold, and how character codes such as ASCII and Unicode store text.

Number systems: binary, denary and hexadecimal

Why computers use two states

A computer stores and moves data as physical states: a high or low voltage on a wire, a charged or uncharged cell in memory, a magnetised region pointing one way or the other on a disk. Circuits that only need to tell two states apart are cheap to build and very reliable, because a small amount of electrical noise does not turn a "high" into a "low". Ten distinguishable voltage levels, one per decimal digit, would be far more sensitive to noise, so computers build every value from many two-state digits instead.

Definition

Bit: a binary digit, the smallest unit of data, which has one of two values, 0 or 1.

The word comes from binary digit.

A single bit can only say "yes" or "no". Groups of bits can represent many more values: each extra bit doubles the number of possible patterns, because every existing pattern can be followed by either a 0 or a 1. Two bits give four patterns (00, 01, 10, 11), three bits give eight, and nnn bits give 2n2^{n}2n.

number of patterns=2nwhere n is the number of bits\text{number of patterns} = 2^{n} \quad \text{where } n \text{ is the number of bits}number of patterns=2nwhere n is the number of bits

Definition

Byte: a group of 8 bits, the standard unit for measuring storage and for addressing memory.

One byte has 28=2562^{8} = 25628=256 possible patterns.

Memory is organised in bytes: each byte has its own address, and larger values occupy several consecutive bytes. Half a byte also has a name, because it matches one hexadecimal digit exactly.

Definition

Nibble: a group of 4 bits, half a byte, which can represent 16 values and is written as one hexadecimal digit.

Place value in three number bases

People normally count in base 10, using ten digits (0 to 9). The value of a digit depends on its position: in 352 the 3 is worth 300 because it sits in the hundreds column. Binary and hexadecimal use the same positional idea with a different base.

Definition

Denary: the base-10 number system, which uses the ten digits 0 to 9 and column values that are powers of 10.

Denary is also called decimal; the name "denary" avoids confusion with decimal fractions.

In any base, each column is worth the base times the column to its right. The rightmost column is always worth 1 (the base to the power 0).

Definition

Place value: the value of a digit's position in a number, equal to the base raised to the power of the position, counting from 0 at the right.

In denary the column values are 1, 10, 100 and 1000. The two number systems used in computing follow the same pattern with bases 2 and 16.

Definition

Binary: the base-2 number system, which uses only the digits 0 and 1 and column values that are powers of 2 (1, 2, 4, 8, 16, …).

Binary maps directly onto two-state hardware, but long strings of 0s and 1s are hard for people to read, copy and compare. Hexadecimal gives a compact way of writing the same bit patterns.

Definition

Hexadecimal: the base-16 number system, which uses sixteen digits, 0 to 9 and A to F (A = 10, B = 11, C = 12, D = 13, E = 14, F = 15), and column values that are powers of 16.

Programmers use hexadecimal as a shorthand for binary because each hex digit stands for exactly four bits.

The place-value tables show the binary number 10011100 and the hexadecimal number 2AF broken into their columns. To find the value, multiply each digit by its column value and add the results.

Place-value tables. Binary: columns 128, 64, 32, 16, 8, 4, 2, 1 (2⁷ to 2⁰) hold 10011100, giving 128 + 16 + 8 + 4 = 156. Hexadecimal: columns 256, 16, 1 hold 2, A, F, giving 512 + 160 + 15 = 687.
Each binary column is worth twice the one to its right and each hexadecimal column sixteen times; a hex digit above 9 is replaced by its value (A = 10, F = 15) before multiplying.

The table of digit values is worth knowing by heart, because every conversion between binary and hexadecimal uses it.

Denary

Binary (4 bits)

Hex

Denary

Binary (4 bits)

Hex

0

0000

0

8

1000

8

1

0001

1

9

1001

9

2

0010

2

10

1010

A

3

0011

3

11

1011

B

4

0100

4

12

1100

C

5

0101

5

13

1101

D

6

0110

6

14

1110

E

7

0111

7

15

1111

F

Within a binary number, the two ends have special names. The leftmost bit carries the largest place value and the rightmost bit the smallest, so a change in the leftmost bit changes the value most.

Definition

Most significant bit (MSB): the leftmost bit of a binary number, which has the largest place value; the rightmost bit, with place value 1, is the least significant bit (LSB).

In the 8-bit number 10011100 the MSB is worth 128 and the LSB is worth 1.

Converting binary to denary

To convert binary to denary, write the place values above the bits, starting with 1 on the right and doubling to the left, then add the place values of the columns that hold a 1. Columns holding 0 contribute nothing. Writing the number in groups of four (1011 0110) makes long numbers easier to read and does not change the value.

Worked example

Calculate the denary value of the 8-bit binary number 1011 0110. [2]

Show the solution

Write the place values over the bits, from 128 on the left to 1 on the right.

128

64

32

16

8

4

2

1

1

0

1

1

0

1

1

0

Add the place values where the bit is 1.

128+32+16+4+2=182128 + 32 + 16 + 4 + 2 = 182128+32+16+4+2=182

Check: an 8-bit number lies between 0 and 255, and the MSB is 1, so the value must be at least 128. 182 is in range.

1011 0110 = 182

Converting denary to binary

There are two reliable methods. The subtraction method works from the left: find the largest place value that fits into the number, put a 1 in that column, subtract, and repeat with the remainder, putting 0 in every column that does not fit. The repeated division method works from the right: divide by 2 again and again, writing down each remainder, until the quotient reaches 0; the remainders read from the last to the first give the binary number.

Left: 156 divided by 2 repeatedly, remainders 0, 0, 1, 1, 1, 0, 0, 1, read from the bottom up as 10011100. Right: 1101011110 grouped in fours from the right as 0011 0101 1110, giving 3, 5, E, so 35E in hexadecimal, which is 862.
Repeated division produces the least significant bit first, so the remainders are read upwards; grouping for hexadecimal always starts at the right, with any padding zeros added on the left.

The division method works because each remainder records whether the current number is odd or even, which is exactly the value of its rightmost bit. Dividing by 2 then shifts every remaining bit one place to the right, so the next remainder gives the next bit. The same reasoning with 16 instead of 2 converts denary to hexadecimal, with each remainder (0 to 15) written as one hex digit.

Worked example

Calculate the binary and hexadecimal representations of the denary number 200. [3]

Show the solution

Subtraction method. The place values are 128, 64, 32, 16, 8, 4, 2, 1.

  • 128 fits into 200: write 1, and 200 − 128 = 72 remains.

  • 64 fits into 72: write 1, and 72 − 64 = 8 remains.

  • 32 and 16 do not fit into 8: write 0, 0.

  • 8 fits: write 1, and 0 remains.

  • 4, 2 and 1: write 0, 0, 0.

So 200 = 1100 1000 in binary.

Repeated division gives the same answer: 200 ÷ 2 = 100 r 0, 100 ÷ 2 = 50 r 0, 50 ÷ 2 = 25 r 0, 25 ÷ 2 = 12 r 1, 12 ÷ 2 = 6 r 0, 6 ÷ 2 = 3 r 0, 3 ÷ 2 = 1 r 1, 1 ÷ 2 = 0 r 1. Reading the remainders from the bottom up gives 11001000.

Hexadecimal. Divide by 16: 200 ÷ 16 = 12 remainder 8, and 12 ÷ 16 = 0 remainder 12. Reading upwards gives 12 and 8, and 12 is the hex digit C, so 200 = C8.

Check by grouping the binary: 1100 = 12 = C and 1000 = 8, which gives C8 again.

Common mistake

Reading the remainders of repeated division from top to bottom. The first remainder is the least significant bit, so writing the remainders in the order they were found reverses the number: for 156 this gives 00111001 (57) instead of 10011100 (156). Read the remainders from the last one found to the first, or check the answer by converting it back.

Converting hexadecimal to and from denary

Converting hexadecimal to denary uses place values of 1, 16, 256 and 4096. Each digit is first turned into its value (A = 10 up to F = 15), multiplied by its place value, and the products are added. Converting denary to hexadecimal uses repeated division by 16, or the subtraction method with multiples of 256 and 16.

2AF16=2×256+10×16+15×1=512+160+15=687\text{2AF}_{16} = 2 \times 256 + 10 \times 16 + 15 \times 1 = 512 + 160 + 15 = 6872AF16​=2×256+10×16+15×1=512+160+15=687

A subscript shows the base when it might be unclear: 2AF16\text{2AF}_{16}2AF16​, 10011100210011100_{2}100111002​, 15610156_{10}15610​. Programming languages use prefixes instead, such as 0x2AF for hexadecimal and 0b10011100 for binary in both Python and Java, and web colours use a leading #.

Converting between binary and hexadecimal

Because 16=2416 = 2^{4}16=24, one hexadecimal digit corresponds to exactly one nibble. Binary converts to hexadecimal by splitting the bits into groups of four starting from the right, padding the leftmost group with zeros if needed, and replacing each group with its hex digit. Hexadecimal converts to binary by replacing each hex digit with its four bits. No denary arithmetic is needed in either direction, which is why hexadecimal is so convenient for writing binary data.

Worked example

(a) Calculate the hexadecimal representation of the 10-bit binary number 1101011110. [2]

(b) Calculate the binary representation of the hexadecimal number 3F7. [1]

Show the solution

(a) Group the bits in fours from the right. Ten bits make two full groups and a group of two on the left, which is padded with two zeros.

11 0101 1110  →  0011  0101  111011\,0101\,1110 \;\rightarrow\; 0011\;0101\;11101101011110→001101011110

Replace each group: 0011 = 3, 0101 = 5, 1110 = 14 = E. So 1101011110 = 35E.

Check through denary: 3×256+5×16+14=768+80+14=8623 \times 256 + 5 \times 16 + 14 = 768 + 80 + 14 = 8623×256+5×16+14=768+80+14=862, and the binary place values give 512+256+64+16+8+4+2=862512 + 256 + 64 + 16 + 8 + 4 + 2 = 862512+256+64+16+8+4+2=862. Both agree.

(b) Replace each hex digit with four bits: 3 = 0011, F = 1111, 7 = 0111. So 3F7 = 0011 1111 0111, which is 11 1111 0111 without the leading zeros.

Common mistake

Grouping bits in fours from the left when the number of bits is not a multiple of four. Splitting 1101011110 from the left gives 1101 0111 10, and padding the last group on the right (1000) changes the value completely. Leading zeros never change a number, so padding belongs on the left, and grouping always starts from the right.

The range of an n-bit unsigned integer

Hardware stores numbers in a fixed number of bits: an 8-bit register, a 32-bit memory location, a 64-bit word. With nnn bits and no sign, the smallest value is all zeros (0) and the largest is all ones, which is one less than the next power of 2.

Definition

Unsigned integer: a whole number stored in binary without a sign, so it can only be zero or positive; with nnn bits it can hold values from 0 to 2n−12^{n} - 12n−1.

range of an n-bit unsigned integer=0 to 2n−1(2n different values)\text{range of an } n\text{-bit unsigned integer} = 0 \text{ to } 2^{n} - 1 \qquad (2^{n} \text{ different values})range of an n-bit unsigned integer=0 to 2n−1(2n different values)

Bits

Number of values

Largest unsigned value

Typical use

4

16

15

one hex digit

8

256

255

one byte; one colour channel of a pixel

16

65,536

65,535

a network port number

32

4,294,967,296

4,294,967,295

an IPv4 address

64

about 1.8×10191.8 \times 10^{19}1.8×1019

18,446,744,073,709,551,615

a memory address on a 64-bit processor

The same formula also works backwards. The number of bits needed to give every item a different code is the smallest nnn for which 2n2^{n}2n is at least the number of items.

Worked example

A school wants to give each of its 1,000 students a unique ID number stored as an unsigned binary integer.

(a) Calculate the minimum number of bits needed. [2]

(b) State the largest ID that this number of bits can hold. [1]

Show the solution

(a) Try powers of 2: 29=5122^{9} = 51229=512, which is fewer than 1,000, and 210=10242^{10} = 1024210=1024, which is at least 1,000. So 10 bits are needed.

(b) The largest value is 210−1=2^{10} - 1 =210−1= 1023 (IDs 0 to 1023 give 1,024 different codes).

A common slip is to answer 1024 in part (b): the 1,024 values start at 0, so the largest is 1023.

Where hexadecimal appears

Hexadecimal appears wherever people need to read or type raw binary values. In each case the stored data is binary, and hexadecimal is only the written form shown to people.

  • Colour codes in HTML and CSS: #FF8800 is three bytes, red FF (255), green 88 (136) and blue 00 (0), an orange. Six hex digits are 24 bits, so this format can express 224=16,777,2162^{24} = 16{,}777{,}216224=16,777,216 colours.

  • MAC addresses of network cards: 48 bits written as twelve hex digits in pairs, such as 3C:22:FB:9A:10:7E.

  • Memory addresses in error messages and debuggers, such as 0x7FFE0000, where eight hex digits replace thirty-two bits.

  • IPv6 addresses: 128 bits written as eight groups of four hex digits.

  • Character codes: Unicode code points are written in hex, such as U+20AC for the euro sign.

Linking question: How do binary and hexadecimal relate to network communication (subtopic A2.3, Data transmissions)? An IP address is a binary number: IPv4 uses 32 bits, shown in dotted denary such as 192.168.0.1 (one denary number per byte), and IPv6 uses 128 bits, shown in hexadecimal because 32 hex digits are far shorter than 128 bits or 16 denary numbers. A subnet mask is applied bit by bit with AND, the same operation as the AND gate.

Conversions in code

Python and Java both convert between bases with built-in functions, but hand-written versions show the methods step by step. The Python program converts denary to any base up to 16 by repeated division, and converts back by adding digit × place value from the right.

Python
DIGITS = "0123456789ABCDEF"


def denary_to_base(n, base):
    if n == 0:
        return "0"
    result = ""
    while n > 0:
        remainder = n % base
        result = DIGITS[remainder] + result
        n = n // base
    return result


def base_to_denary(text, base):
    total = 0
    place = 1
    for i in range(len(text) - 1, -1, -1):
        total = total + DIGITS.index(text[i]) * place
        place = place * base
    return total


print(denary_to_base(156, 2), denary_to_base(156, 16))
print(base_to_denary("10011100", 2), base_to_denary("2AF", 16))
print(format(156, "08b"), format(156, "X"), int("2AF", 16))
Java version
Java
public class Convert {
    static final String DIGITS = "0123456789ABCDEF";

    static String denaryToBase(int n, int base) {
        if (n == 0) {
            return "0";
        }
        String result = "";
        while (n > 0) {
            int remainder = n % base;
            result = DIGITS.charAt(remainder) + result;
            n = n / base;
        }
        return result;
    }

    static int baseToDenary(String text, int base) {
        int total = 0;
        int place = 1;
        for (int i = text.length() - 1; i >= 0; i--) {
            total = total + DIGITS.indexOf(text.charAt(i)) * place;
            place = place * base;
        }
        return total;
    }

    public static void main(String[] args) {
        System.out.println(denaryToBase(156, 2) + " " + denaryToBase(156, 16));
        System.out.println(baseToDenary("10011100", 2) + " " + baseToDenary("2AF", 16));
        String padded = String.format("%8s", Integer.toBinaryString(156)).replace(' ', '0');
        System.out.println(padded + " " + Integer.toHexString(156).toUpperCase() + " " + Integer.parseInt("2AF", 16));
    }
}

Each remainder is placed in front of the result built so far, which is how the program reads the remainders "from the bottom up" without storing them. The last line of each version uses the built-ins: Python's format(n, "08b") pads to 8 bits and int(text, 16) converts from hex; Java's Integer.toBinaryString drops leading zeros (hence the padding with String.format), Integer.toHexString returns lower-case letters, and Integer.parseInt(text, 16) converts back. In Java, / between two int values is integer division, the same as // in Python.

Plain text
10011100 9C
156 687
10011100 9C 687

Tip

To check a binary conversion quickly, compare the last bit with the number: it is 1 exactly when the number is odd. 156 is even, so its binary form must end in 0; 687 is odd, so 2AF in binary must end in 1 (F = 1111).

How binary stores data

Bits only have meaning through an encoding

A pattern of bits has no meaning on its own. The byte 0100 0001 is the integer 65 to a program expecting a number, the letter A to a program expecting text, and a dark-grey pixel to a program expecting an 8-bit greyscale image. The program that reads the data must therefore know which encoding was used to write it, and each data type in a programming language is an agreement about how many bits a value occupies and what they mean.

This has direct effects on storage and retrieval. A file opened with the wrong encoding shows nonsense characters; a number read as text cannot be used in arithmetic until it is converted; and data written by one system can only be read by another if both follow the same format. File formats such as PNG, WAV and MP4 therefore begin with a header that states how the following bits are to be interpreted.

Units of storage

Storage sizes are counted in bytes with prefixes. Two systems of prefixes exist, which causes confusion in calculations and in the advertised capacity of drives.

Definition

Decimal prefix: a unit prefix based on powers of 1000, as in kilobyte (kB, 1000 bytes), megabyte (MB, 100021000^{2}10002 bytes), gigabyte (GB, 100031000^{3}10003 bytes) and terabyte (TB, 100041000^{4}10004 bytes).

Memory chips are built in sizes that are powers of 2, so a second set of prefixes based on 1024 is used for exact binary sizes.

Definition

Binary prefix: a unit prefix based on powers of 1024, as in kibibyte (KiB, 1024 bytes), mebibyte (MiB, 102421024^{2}10242 bytes) and gibibyte (GiB, 102431024^{3}10243 bytes).

The International Electrotechnical Commission introduced these names in 1998 to remove the ambiguity of "kilobyte".

Decimal prefix

Bytes

Binary prefix

Bytes

1 kB

1,000

1 KiB

1,024

1 MB

1,000,000

1 MiB

1,048,576

1 GB

1,000,000,000

1 GiB

1,073,741,824

1 TB

101210^{12}1012

1 TiB

1,099,511,627,776

The gap grows with each step: a gibibyte is about 7% larger than a gigabyte. A drive sold as 1 TB holds 101210^{12}1012 bytes, which an operating system that reports in binary units shows as about 931 "GB" (really GiB), so the drive appears smaller than advertised although no space is missing.

Common mistake

Mixing bits and bytes, or 1000 and 1024, in one calculation. A file size worked out in bits must be divided by 8 to give bytes, and the final unit must match the divisor used: dividing by 1,000,000 gives MB, dividing by 1,048,576 gives MiB. Write the unit at every step (b for bits, B for bytes) so the slip shows up.

Integers and overflow

A whole number that cannot be negative is stored as an unsigned binary number in a fixed number of bytes, chosen in advance by the data type. Using more bits allows larger values but costs more memory for every value stored, so systems choose the smallest width that is safe for the data.

Definition

Overflow: the error that occurs when the result of a calculation is too large to be represented in the fixed number of bits available.

When overflow happens in hardware, the extra carry bit has nowhere to go and is lost, so the stored result "wraps round" to a small number. In an 8-bit register, 200 + 100 = 300 needs nine bits (1 0010 1100); only the lower eight bits (0010 1100 = 44) are kept. The processor records the lost bit in its carry flag, which software can check; a program that ignores the flag continues with a wrong value.

1100 1000+  0110 01001  0010 11008 bits keep 0010 1100=44, not 300\begin{array}{r} 1100\,1000 \\ +\;0110\,0100 \\ \hline 1\;0010\,1100 \end{array} \qquad \text{8 bits keep } 0010\,1100 = 44, \text{ not } 30011001000+01100100100101100​​8 bits keep 00101100=44, not 300

The Year 2038 problem is a real case. Many systems store time as the number of seconds since 00:00:00 UTC on 1 January 1970 in a signed 32-bit integer, whose largest value is 231−1=2,147,483,6472^{31} - 1 = 2{,}147{,}483{,}647231−1=2,147,483,647. That count is reached at 03:14:07 UTC on 19 January 2038; one second later the value overflows, and affected systems read the date as December 1901. Moving to 64-bit time values removes the problem, which is why operating systems and databases have been changing their time types.

231−1 seconds365.25×24×60×60 seconds per year≈68.05 years after 1970\frac{2^{31} - 1 \text{ seconds}}{365.25 \times 24 \times 60 \times 60 \text{ seconds per year}} \approx 68.05 \text{ years after 1970}365.25×24×60×60 seconds per year231−1 seconds​≈68.05 years after 1970

Source: Year 2038 problem, Wikipedia, October 2026.

How each language deals with overflow differs: Java's int is fixed at 32 bits and wraps round, while Python's int grows to any size; subtopic B2.1, Programming fundamentals, shows both in code for the integer data type.

Extension: negative integers and two's complement

This short extension goes beyond unsigned integers. Negative numbers need a different representation, and one method is so widely used that an outline helps explain the signed values in the Year 2038 problem and in Java's int.

Definition

Two's complement: a way of storing signed integers in which the most significant bit has a negative place value, so an nnn-bit number holds values from −2n−1-2^{n-1}−2n−1 to 2n−1−12^{n-1} - 12n−1−1.

In 8-bit two's complement the MSB is worth −128 instead of +128, so 1111 1111 is −128+127=−1-128 + 127 = -1−128+127=−1 and the range is −128 to 127. To negate a number, invert every bit and add 1: 5 is 0000 0101, inverting gives 1111 1010, and adding 1 gives 1111 1011, which is −5. The same adder circuit then handles both positive and negative numbers, which is why processors use this method.

Characters and character sets

Text is stored by giving every character a number and storing the number in binary. The keyboard, the program, the file and the screen all have to use the same list of numbers, or the text is shown as the wrong symbols.

Definition

Character set: an agreed list of characters in which each character is given a unique number, so that text can be stored and exchanged as binary.

Tip

A useful way to keep the vocabulary straight: the character set is the whole table, a character code is one entry in it, and an encoding is the rule for turning that number into bytes. In ASCII the code and the stored byte are the same; in UTF-8 they can differ.

Definition

Character code: the number that a character set assigns to one character, stored in binary to represent that character.

The number of bits used for each code limits how many characters a set can contain: nnn bits allow 2n2^{n}2n codes. The most influential early set was designed with 7 bits.

Definition

ASCII: the American Standard Code for Information Interchange, a 7-bit character set of 128 codes covering the English alphabet, digits, punctuation and control characters.

Of ASCII's 128 codes, 95 are printable characters (letters, digits, punctuation and the space) and 33 are control characters, such as line feed (code 10), which marks the end of a line, and delete (code 127). The excerpt shows how the codes are laid out.

Character

Denary code

7-bit binary

Hex

line feed (control)

10

000 1010

0A

9

57

011 1001

39

?

63

011 1111

3F

B

66

100 0010

42

Z

90

101 1010

5A

b

98

110 0010

62

~

126

111 1110

7E

Source: ASCII, Wikipedia, October 2026.

The layout follows patterns that make processing easier. Letters are in alphabetical order, so comparing codes sorts words; the digits 0 to 9 have codes 48 to 57, so a digit's value is its code minus 48; and each lower-case letter has the bit with place value 32 set, while its capital does not (B is 66 and b is 98). Comparing codes also explains why sorting by code puts every capital before every lower-case letter, so "Zebra" comes before "apple".

Unicode and UTF-8

ASCII has no codes for accented letters, other alphabets, Chinese characters or mathematical symbols. For decades many incompatible 8-bit sets each added 128 extra characters for one region, so a file written in Greece could not be read correctly in Russia. Unicode replaced them with one set for every writing system.

Definition

Unicode: a universal character set that gives every character of the world's writing systems, together with symbols and emoji, its own number, called a code point.

Unicode code points run from U+0000 to U+10FFFF, room for 1,114,112 code points. Version 18.0 assigns 172,808 characters across 175 scripts, so most of the space is still free. The first 128 code points are identical to ASCII, so every ASCII file is also valid Unicode text.

Source: Unicode, Wikipedia, October 2026.

A code point is a number; it still needs a rule for storing it as bytes. A fixed four bytes per character (UTF-32) would work for every character but would quadruple the size of English text. The most widely used encoding avoids that waste by varying the length.

Definition

UTF-8: a variable-length encoding of Unicode that stores each code point in one, two, three or four bytes, using a single byte for the 128 ASCII characters.

UTF-8 is the encoding used by almost every web page.

The figure shows how UTF-8 marks the length of each character. A byte that starts with 0 is a complete one-byte character. Otherwise the first byte starts with as many 1s as the character has bytes (110 for two, 1110 for three, 11110 for four), and every following byte starts with 10. The remaining bits, unshaded, hold the code point itself.

UTF-8 bytes for four characters with marker bits shaded: $ (U+0024) 00100100, one byte; £ (U+00A3) 11000010 10100011; € (U+20AC) 11100010 10000010 10101100; globe emoji (U+1F30D) 11110000 10011111 10001100 10001101, four bytes.
The marker bits at the start of each byte tell a decoder where every character begins and how many bytes it uses, so text can be read correctly even when characters have different lengths.

The design has three advantages: English text costs no more than in ASCII, old ASCII files are already valid UTF-8, and a decoder that starts reading in the middle of a file can find the next character boundary by skipping bytes that begin with 10. The cost is that the number of characters can no longer be found from the file size, and characters outside ASCII take two to four bytes, so text in Chinese or Hindi takes more space than in a fixed 16-bit encoding for those scripts.

Strings

A string is stored as a sequence of character codes in consecutive memory locations, together with a way of knowing where it ends: either a stored length or a special terminating code. Every character, including spaces and punctuation, occupies storage, and a digit inside a string is stored as its character code, not as its value. The text "255" is three characters (codes 50, 53, 53), three bytes in ASCII or UTF-8, while the integer 255 fits in a single byte as 1111 1111.

The Python program prints each character of a short string with its code point and its UTF-8 bytes in hexadecimal, then compares the number of characters with the number of bytes.

Python
text = "£4 or €5"
total_bytes = 0
for ch in text:
    code = ord(ch)
    encoded = ch.encode("utf-8")
    total_bytes = total_bytes + len(encoded)
    print(repr(ch), "U+" + format(code, "04X"), encoded.hex(" ").upper(), len(encoded))
print(len(text), "characters,", total_bytes, "bytes")
Java version
Java
import java.nio.charset.StandardCharsets;

public class Chars {
    public static void main(String[] args) {
        String text = "£4 or €5";
        int totalBytes = 0;
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            byte[] encoded = String.valueOf(ch).getBytes(StandardCharsets.UTF_8);
            totalBytes = totalBytes + encoded.length;
            String hexBytes = "";
            for (int j = 0; j < encoded.length; j++) {
                hexBytes = hexBytes + String.format("%02X ", encoded[j] & 0xFF);
            }
            System.out.println("'" + ch + "' U+" + String.format("%04X", (int) ch) + " "
                    + hexBytes.trim() + " " + encoded.length);
        }
        System.out.println(text.length() + " characters, " + totalBytes + " bytes");
    }
}

ord (Python) and the cast (int) ch (Java) give the code point; encode("utf-8") and getBytes(StandardCharsets.UTF_8) give the stored bytes. Java's byte type is signed, so & 0xFF turns each byte into its unsigned value 0 to 255 before it is printed in hex. Java also stores a char in 16 bits, which is enough for £ and € but not for a character such as the globe emoji, which needs two char values; the Python version has no such limit.

Plain text
'£' U+00A3 C2 A3 2
'4' U+0034 34 1
' ' U+0020 20 1
'o' U+006F 6F 1
'r' U+0072 72 1
' ' U+0020 20 1
'€' U+20AC E2 82 AC 3
'5' U+0035 35 1
8 characters, 11 bytes

Worked example

A messaging app stores each message in UTF-8. One message reads Zoë: 20€.

(a) Calculate the number of bytes the message occupies. [2]

(b) Explain why the app's developers cannot assume that a 1,000-byte message contains 1,000 characters. [2]

Show the solution

(a) Go through the characters. Z, o, :, the space, 2 and 0 are ASCII characters, one byte each: 6 bytes. ë (U+00EB) is in the range U+0080 to U+07FF, so it takes 2 bytes. € (U+20AC) is in the range U+0800 to U+FFFF, so it takes 3 bytes.

6×1+1×2+1×3=11 bytes6 \times 1 + 1 \times 2 + 1 \times 3 = 11 \text{ bytes}6×1+1×2+1×3=11 bytes

The message has 8 characters but occupies 11 bytes.

(b) UTF-8 is a variable-length encoding: a character takes between one and four bytes depending on its code point [1]. So 1,000 bytes could hold anything from 250 four-byte characters to 1,000 ASCII characters, and the number of characters can only be found by decoding the bytes [1].

Common mistake

Assuming ASCII can store any text. ASCII has only 128 codes and covers English letters only, so names such as Zoë, Łukasz or 李明 cannot be stored in it. Systems that handle names, addresses or messages from many countries use Unicode, normally encoded as UTF-8.

Images as bitmaps

A digital photograph or screenshot is stored as a grid of tiny squares, each of a single colour. The program that displays the image only needs to know the size of the grid and the colour of every square.

Definition

Pixel: the smallest single-coloured element of a bitmap image or display; its name is short for "picture element".

A photo from a phone camera contains millions of pixels, each too small to see on its own at normal viewing distance. Stored in order, row by row, their values make up the image.

Definition

Bitmap image: an image stored as a grid of pixels, with a binary value recording the colour of each pixel.

Two settings decide how much detail a bitmap holds and how much storage it needs: how many pixels it has, and how many bits describe each pixel.

Definition

Image resolution: the number of pixels in an image, usually given as width × height.

A full HD image is 1920 × 1080, which is 2,073,600 pixels.

Tip

"Resolution" has a second meaning for printers and scanners: pixels or dots per inch (ppi or dpi), the density of pixels on paper. In storage calculations it means the pixel dimensions, width × height.

Definition

Colour depth: the number of bits used to store the colour of each pixel; a colour depth of nnn bits allows 2n2^{n}2n different colours.

The figure stores the same small disc at two colour depths. With 1 bit per pixel each pixel can only be black or white, so the curved edge becomes a staircase. With 8 bits per pixel, 256 shades of grey are available, and the edge pixels take in-between shades that make the outline look smooth from a distance. The 8-bit version uses eight times as much storage.

The same 8 by 8 disc stored two ways. At 1 bit per pixel each pixel is black (1) or white (0); row 4 is 01111110; 64 bits = 8 bytes. In 8-bit greyscale edge pixels are grey; one is 01001101 = 77; 512 bits = 64 bytes.
Each extra bit of colour depth doubles the number of shades available, and the file grows in proportion to the bits per pixel.

Colour images normally store three values per pixel, the intensities of red, green and blue light (RGB). With one byte per channel the colour depth is 24 bits, enough for 224=16,777,2162^{24} = 16{,}777{,}216224=16,777,216 colours, which gives smooth shading in photographs. This is the same 24-bit format as the web's hex colour codes: #FF8800 is the pixel value R = 255, G = 136, B = 0.

Colour depth

Colours

Typical use

1 bit

2

black-and-white icons, fax

8 bits

256

greyscale photos, simple GIF graphics

16 bits

65,536

older phone and game screens

24 bits

16,777,216

photographs, web colours (#RRGGBB)

The uncompressed size of a bitmap follows directly from the definitions: every pixel needs as many bits as the colour depth.

image size (bits)=width×height×colour depthimage size (bytes)=width×height×colour depth8\text{image size (bits)} = \text{width} \times \text{height} \times \text{colour depth} \qquad \text{image size (bytes)} = \frac{\text{width} \times \text{height} \times \text{colour depth}}{8}image size (bits)=width×height×colour depthimage size (bytes)=8width×height×colour depth​

Worked example

A camera app saves an uncompressed photo with a resolution of 1920 × 1080 pixels and a colour depth of 24 bits.

(a) Calculate the file size in megabytes (MB), ignoring metadata. [2]

(b) The app offers an 8-bit greyscale mode. State how many shades this mode can show and calculate the saving in storage. [2]

Show the solution

(a) Number of pixels: 1920×1080=2,073,6001920 \times 1080 = 2{,}073{,}6001920×1080=2,073,600.

2,073,600×24=49,766,400 bits2{,}073{,}600 \times 24 = 49{,}766{,}400 \text{ bits}2,073,600×24=49,766,400 bits
49,766,400÷8=6,220,800 bytes=6.2208 MB≈6.2 MB49{,}766{,}400 \div 8 = 6{,}220{,}800 \text{ bytes} = 6.2208 \text{ MB} \approx 6.2 \text{ MB}49,766,400÷8=6,220,800 bytes=6.2208 MB≈6.2 MB

(In binary units, 6,220,800÷1,048,576≈5.936{,}220{,}800 \div 1{,}048{,}576 \approx 5.936,220,800÷1,048,576≈5.93 MiB.)

(b) 8 bits give 28=2562^{8} = 25628=256 shades. Each pixel now needs 1 byte instead of 3, so the file is 2,073,6002{,}073{,}6002,073,600 bytes ≈2.1\approx 2.1≈2.1 MB, a saving of 6,220,800−2,073,600=4,147,2006{,}220{,}800 - 2{,}073{,}600 = 4{,}147{,}2006,220,800−2,073,600=4,147,200 bytes, two thirds of the original size.

Metadata

An image file also stores information about the image, so that software can decode the pixels and organise the files.

Definition

Metadata: data that describes other data, recording the properties needed to interpret, organise or find it.

An image's metadata includes its width, height, colour depth and file format, and often the date and place the photo was taken.

Without the width, height and colour depth, a program could not tell where one row of pixels ends or how many bits belong to each pixel, so metadata is essential for retrieval. Photos often also carry EXIF metadata added by the camera: the device model, exposure settings, the date and time, and often GPS coordinates. Sharing a photo with its location data can reveal where someone lives or works, which is why many social media platforms remove this metadata when a photo is uploaded and why phones offer a setting to leave location out.

Vector graphics

Not every image is stored as pixels. Logos, icons, fonts, maps and diagrams are often stored as a list of shapes instead.

Definition

Vector graphic: an image stored as a list of shapes (lines, curves, polygons and text) described by coordinates and properties such as colour and line width, which the computer draws when the image is displayed.

Because the shapes are recalculated at display time, a vector graphic can be scaled to any size without becoming blocky, and its file size depends on the number of shapes rather than the number of pixels. Web pages use vector files (SVG) for icons and logos for that reason, so they stay sharp on every screen size. Vectors cannot represent photographs efficiently, because a photo has no simple shapes, only millions of slightly different pixels.

Feature

Bitmap image

Vector graphic

Stored as

colour of every pixel

shapes with coordinates and properties

File size depends on

resolution × colour depth

number and complexity of shapes

Enlarging

becomes blocky (pixelated)

stays sharp at any size

Suited to

photographs, scanned images

logos, icons, fonts, diagrams, maps

Editing

change individual pixels

move, resize or recolour whole shapes

Examples of formats

PNG, JPEG, GIF, BMP

SVG, PDF drawings, font outlines

Sound: from analogue to digital

Sound is a continuous vibration of air pressure. A microphone turns it into an electrical voltage that rises and falls smoothly with the pressure, and can take any value within its range.

Definition

Analogue signal: a signal that varies continuously, so it can take any value within a range at any moment in time.

Data inside a computer takes the opposite form, built from a fixed set of values.

Definition

Digital signal: a signal that can take only a limited set of discrete values, such as the binary values 0 and 1, at discrete moments in time.

A computer can only store a finite list of numbers, so the continuous signal must be measured at regular moments, and each measurement rounded to a whole number that fits in a fixed number of bits. The hardware that does this is a converter at the boundary between the analogue world and the computer.

Definition

Analogue-to-digital converter (ADC): a circuit that measures an analogue signal at regular intervals and outputs each measurement as a binary number.

An ADC carries out two steps for every measurement: it captures the signal's value at a fixed moment, then rounds that value to the nearest level it can store in its fixed number of bits.

Definition

Sampling: measuring the amplitude of an analogue signal at regular, equally spaced moments and storing each measurement, called a sample, as a binary number.

The waveform shows the process. The smooth curve is the analogue signal; the dashed lines mark the sampling moments; each dot is the stored value, rounded to the nearest of eight levels because each sample has only 3 bits. The stored audio is just the list of sample values.

An analogue sound wave measured at 16 equally spaced times. Each sample is rounded to one of eight 3-bit levels, 000 to 111. Stored values: 5 6 6 5 5 5 4 2 1 0 1 3 4 4 5 6. The gap between samples is 1 divided by the sampling rate.
Detail is lost twice: between samples (set by the sampling rate) and in rounding each sample to a level (set by the bit depth); more samples and more levels both bring the stored values closer to the original curve.

Definition

Sampling rate: the number of samples taken per second, measured in hertz (Hz).

At a sampling rate of 44,100 Hz, every second of sound in each channel becomes 44,100 stored numbers. How precisely each of those numbers is stored is the second setting.

Definition

Bit depth: the number of bits used to store each sample of a sound, which sets how many different amplitude levels (2n2^{n}2n for nnn bits) can be recorded.

A higher sampling rate captures faster changes, so higher-pitched sounds survive. A sampling rate must be more than twice the highest frequency to be recorded (the Nyquist–Shannon sampling theorem), and human hearing extends to about 20,000 Hz, so music needs a rate above 40,000 Hz. A greater bit depth gives finer levels, so the rounding error (heard as a faint hiss called quantisation noise) becomes smaller and quiet passages keep their detail. Both improvements cost storage in direct proportion: doubling the sampling rate or the bit depth doubles the file size.

Playing the sound reverses the process. The stored numbers are sent to a converter that produces a voltage for each one, smoothed into a continuous wave and fed to a speaker.

Definition

Digital-to-analogue converter (DAC): a circuit that turns a sequence of binary numbers into a continuously varying analogue signal.

The uncompressed size of a recording depends on four values. Stereo sound has two channels (left and right), each sampled separately.

audio size (bits)=sampling rate (Hz)×bit depth×channels×time (s)\text{audio size (bits)} = \text{sampling rate (Hz)} \times \text{bit depth} \times \text{channels} \times \text{time (s)}audio size (bits)=sampling rate (Hz)×bit depth×channels×time (s)

Common mistake

Thinking a higher sampling rate improves quality at no cost. Each extra sample per second adds bit depth × channels bits for every second of sound, so the file size grows in direct proportion to the sampling rate. Beyond about twice the highest audible frequency the extra samples add storage without any change a listener can hear.

Case study: CD audio and the Red Book standard

The compact disc, launched by Sony and Philips in the early 1980s, stores sound in the format set out in the "Red Book" specification: pulse-code modulation (uncompressed samples) at 44,100 samples per second, 16 bits per sample, two channels, with up to 74 minutes per disc in the original specification. Each choice follows from the ideas of sampling.

Setting

Red Book value

Reason

Sampling rate

44,100 Hz

more than twice the 20,000 Hz limit of human hearing

Bit depth

16 bits

216=65,5362^{16} = 65{,}536216=65,536 levels, low enough noise for quiet music

Channels

2

stereo: separate left and right signals

Data rate

176,400 bytes per second

44,100×16×2÷844{,}100 \times 16 \times 2 \div 844,100×16×2÷8

Original playing time

up to 74 minutes

Source: Compact Disc Digital Audio, Wikipedia, October 2026.

The rate sits about 4,000 Hz above the 40,000 Hz minimum because the analogue filter that removes frequencies above 20,000 Hz before sampling cannot cut off sharply; the margin gives the filter room to work. The exact number was inherited from the equipment used to carry digital audio from studios to CD factories at the time, which recorded samples on video tape. The 16-bit depth was the result of a debate between the two companies, in which Philips had already built a 14-bit converter and Sony pressed for 16 bits.

Worked example

Calculate the storage needed for one minute of CD audio, and for a full 74-minute disc. Give the answers in MB. [3]

Show the solution

Bits per second: 44,100×16×2=1,411,20044{,}100 \times 16 \times 2 = 1{,}411{,}20044,100×16×2=1,411,200 bits.

One minute is 60 seconds:

1,411,200×60=84,672,000 bits1{,}411{,}200 \times 60 = 84{,}672{,}000 \text{ bits}1,411,200×60=84,672,000 bits
84,672,000÷8=10,584,000 bytes≈10.6 MB per minute84{,}672{,}000 \div 8 = 10{,}584{,}000 \text{ bytes} \approx 10.6 \text{ MB per minute}84,672,000÷8=10,584,000 bytes≈10.6 MB per minute

A full disc:

10,584,000×74=783,216,000 bytes≈783 MB10{,}584{,}000 \times 74 = 783{,}216{,}000 \text{ bytes} \approx 783 \text{ MB}10,584,000×74=783,216,000 bytes≈783 MB

Check: the data rate in the Red Book table is 176,400 bytes per second, and 176,400×60=10,584,000176{,}400 \times 60 = 10{,}584{,}000176,400×60=10,584,000. Both routes agree.

These numbers show the trade-off that every audio format makes. CD quality is close to the limits of human hearing, but at about 10.6 MB per minute a music library quickly fills a phone, and streaming it would use a lot of network capacity. Formats for streaming and portable players therefore apply lossy compression, which removes sounds a listener is unlikely to notice and stores the same minute in a small fraction of the space; subtopic A1.1, Computer hardware and operation, explains compression in full. Studio recordings go the other way, using 24-bit samples at 48,000 Hz or more, because editing and mixing benefit from the extra precision even if the final release does not keep it.

Video

A video is a sequence of still bitmap images, called frames, shown quickly enough that the eye sees continuous motion, usually together with an audio track.

Definition

Frame rate: the number of frames (still images) shown per second of video, measured in frames per second (fps).

Common frame rates are 24 fps for cinema, 25 or 30 fps for television and 60 fps for games and sport, where fast motion looks smoother with more frames. The size of uncompressed video combines the bitmap and frame-rate ideas, with the sound added separately.

video size (bits)=width×height×colour depth×frame rate×time (s)  +  audio\text{video size (bits)} = \text{width} \times \text{height} \times \text{colour depth} \times \text{frame rate} \times \text{time (s)} \;+\; \text{audio}video size (bits)=width×height×colour depth×frame rate×time (s)+audio

Uncompressed video is enormous. One minute of 1920 × 1080 video at 24-bit colour and 30 fps needs 6,220,8006{,}220{,}8006,220,800 bytes per frame × 30 frames × 60 seconds = 11,197,440,000 bytes, about 11.2 GB, before any sound. Video is therefore almost always compressed. Consecutive frames are usually very similar, so a video encoder stores some complete frames and, for the frames in between, only what has changed; each frame is also compressed with lossy methods. This often brings the size down by a factor of a hundred or more, at the cost of processing time to encode and decode.

Worked example

A security camera records uncompressed greyscale video at 1280 × 720 pixels, 8-bit colour depth and 25 fps, with no sound.

(a) Calculate the storage needed for 10 seconds of video, in MB. [2]

(b) The owner wants to record in 24-bit colour at 30 fps instead. Calculate the factor by which the storage increases. [2]

Show the solution

(a) Bytes per frame: 1280×720×8÷8=921,6001280 \times 720 \times 8 \div 8 = 921{,}6001280×720×8÷8=921,600 bytes.

921,600×25×10=230,400,000 bytes≈230 MB921{,}600 \times 25 \times 10 = 230{,}400{,}000 \text{ bytes} \approx 230 \text{ MB}921,600×25×10=230,400,000 bytes≈230 MB

(b) The colour depth triples (8 to 24 bits) and the frame rate rises by 30÷25=1.230 \div 25 = 1.230÷25=1.2. Size is proportional to both, so it rises by 3×1.2=3 \times 1.2 =3×1.2= 3.6 times (to about 829 MB for the same 10 seconds).

The size calculations in code are three short subprograms, one per formula, each dividing by 8 to give bytes. In Java the parameters are long (64-bit) because the video result for one minute, 89,579,520,000 bits, is far beyond the largest int value of 2,147,483,647 and would overflow.

Python
def image_bytes(width, height, colour_depth):
    return width * height * colour_depth // 8


def audio_bytes(sample_rate, bit_depth, channels, seconds):
    return sample_rate * bit_depth * channels * seconds // 8


def video_bytes(width, height, colour_depth, frame_rate, seconds):
    return image_bytes(width, height, colour_depth) * frame_rate * seconds


print(image_bytes(1920, 1080, 24))
print(audio_bytes(44100, 16, 2, 60))
print(video_bytes(1920, 1080, 24, 30, 60))
Java version
Java
public class Sizes {
    static long imageBytes(long width, long height, long colourDepth) {
        return width * height * colourDepth / 8;
    }

    static long audioBytes(long sampleRate, long bitDepth, long channels, long seconds) {
        return sampleRate * bitDepth * channels * seconds / 8;
    }

    static long videoBytes(long width, long height, long colourDepth, long frameRate, long seconds) {
        return imageBytes(width, height, colourDepth) * frameRate * seconds;
    }

    public static void main(String[] args) {
        System.out.println(imageBytes(1920, 1080, 24));
        System.out.println(audioBytes(44100, 16, 2, 60));
        System.out.println(videoBytes(1920, 1080, 24, 30, 60));
    }
}
Plain text
6220800
10584000
11197440000

The effect of encoding on storage and retrieval

Every encoding decision balances fidelity against size. More bits per value (a larger integer type, a greater colour depth or bit depth) or more values (a higher resolution, sampling rate or frame rate) give a more accurate representation, but the file grows in proportion, takes longer to read from storage, and takes longer to send over a network.

Decision

Benefit of more bits or values

Cost

Integer width (8, 16, 32, 64 bits)

larger range, less risk of overflow

more memory for every value stored

Character encoding (ASCII vs UTF-8 vs UTF-32)

more languages and symbols

non-ASCII characters take 2–4 bytes; character count no longer equals byte count

Image resolution and colour depth

finer detail, smoother colour

size grows with width × height × depth

Audio sampling rate and bit depth

higher pitches kept, less noise

size grows with rate × depth × channels

Video frame rate

smoother motion

size grows with every extra frame per second

Retrieval depends on the encoding as much as storage does. The reading program must apply exactly the same rules as the writing program (the same character encoding, the same pixel order and colour depth, the same sample format), which is why formats are standardised and files carry metadata describing their contents.

Theory of knowledge: Binary and Boolean logic describe the world with only two values, yet sound, colour and temperature vary continuously. A digital recording is a model that discards information below a chosen level of detail. To what extent is knowledge that has been digitised still knowledge of the original, and who decides which detail is "too small to matter"?

Logic gates

What logic gates do

Storing bits is only half of what a computer does; it also has to combine them, to add two numbers, compare two values or decide which instruction runs next. These operations are built from circuits that take one or more bits as input and produce a bit as output according to a fixed rule. A high voltage represents 1 (true) and a low voltage represents 0 (false), and each circuit is made from a handful of transistors acting as electrically controlled switches.

Definition

Logic gate: an electronic circuit that takes one or more binary inputs and produces a single binary output determined by a Boolean operation.

The rules that gates follow come from a branch of mathematics that works with only two values, true and false. George Boole set it out in his book The Laws of Thought (1854), long before electronic computers. Claude Shannon's master's thesis of 1937 showed that circuits of relay switches could carry out the same algebra, and that idea underlies every digital circuit built since.

Definition

Boolean algebra: the algebra of the two values true (1) and false (0), with operations such as AND, OR and NOT and laws for rewriting and simplifying expressions.

Boolean algebra uses letters for inputs, as ordinary algebra uses letters for numbers, but each letter can only be 0 or 1.

Definition

Boolean expression: an expression built from Boolean variables, the constants 0 and 1, and Boolean operators, whose value is either true (1) or false (0).

The operators in these expressions are the operations that the gates carry out.

Definition

Boolean operator: an operation that combines or changes Boolean values: AND, OR, NOT, NAND, NOR, XOR or XNOR.

The operators can be written in words or in symbols. Both forms appear in textbooks and questions, and the two programming languages have their own operators for the same ideas. The symbol forms make long expressions shorter, so they are used for algebra; the word forms are clearer in descriptions.

Operator

Word form

Symbol form

Python

Java

AND

A AND B

A⋅BA \cdot BA⋅B or ABABAB

a and b

a && b

OR

A OR B

A+BA + BA+B

a or b

a || b

NOT

NOT A

A‾\overline{A}A

not a

!a

NAND

A NAND B

A⋅B‾\overline{A \cdot B}A⋅B

not (a and b)

!(a && b)

NOR

A NOR B

A+B‾\overline{A + B}A+B​

not (a or b)

!(a || b)

XOR

A XOR B

A⊕BA \oplus BA⊕B

a != b (or a ^ b on bits)

a != b (or a ^ b)

XNOR

A XNOR B

A⊕B‾\overline{A \oplus B}A⊕B​

a == b

a == b

The plus sign means OR, not addition: in Boolean algebra 1+1=11 + 1 = 11+1=1. Without brackets, NOT is applied first, then AND, then OR, just as multiplication comes before addition in arithmetic, so A+B⋅CA + B \cdot CA+B⋅C means A+(B⋅C)A + (B \cdot C)A+(B⋅C).

Truth tables for the seven gates

Each gate's behaviour can be shown completely by listing its output for every possible combination of inputs.

Definition

Truth table: a table that lists every possible combination of input values of a logic circuit or Boolean expression, with the output for each combination.

The figure shows the standard distinctive-shape symbol for each of the seven gates, as used on circuit diagrams. The shapes matter: AND has a flat back and a round front, OR has a curved back and a pointed front, a small circle (a "bubble") on an output means the result is inverted, and XOR has an extra curved line behind the OR shape.

Standard distinctive-shape symbols for the seven logic gates AND, OR, NOT, NAND, NOR, XOR and XNOR, each with inputs A and B (only A for NOT), output Q and its expression in words, such as Q = NOT (A AND B) for NAND.
NAND, NOR and XNOR are the AND, OR and XOR shapes with a bubble on the output, which inverts the result.

Definition

AND gate: a logic gate whose output is 1 only when all of its inputs are 1.

A

B

A AND B

0

0

0

0

1

0

1

0

0

1

1

1

An AND gate checks that several conditions hold together, like a door that opens only when a key card is valid AND the time is within opening hours.

Definition

OR gate: a logic gate whose output is 1 when at least one of its inputs is 1.

A

B

A OR B

0

0

0

0

1

1

1

0

1

1

1

1

OR is "inclusive": the output is also 1 when both inputs are 1. A car's interior light that comes on when either front door is open behaves like an OR gate.

Definition

NOT gate: a logic gate with one input whose output is the opposite of the input, 1 for an input of 0 and 0 for an input of 1.

A

NOT A

0

1

1

0

The NOT gate, also called an inverter, is the only one of the seven with a single input.

Definition

NAND gate: a logic gate whose output is 0 only when all of its inputs are 1, equivalent to an AND gate followed by a NOT gate.

A

B

A NAND B

0

0

1

0

1

1

1

0

1

1

1

0

NAND is the exact opposite of AND in every row. In standard chip technology (CMOS) it needs four transistors against six for AND, and every other gate can be made from NAND gates alone.

Definition

NOR gate: a logic gate whose output is 1 only when all of its inputs are 0, equivalent to an OR gate followed by a NOT gate.

A

B

A NOR B

0

0

1

0

1

0

1

0

0

1

1

0

NOR is the exact opposite of OR. Its output is 1 only when nothing is switched on, which suits a signal such as "all clear: no sensor triggered".

Definition

XOR gate: a logic gate whose output is 1 when exactly one of its two inputs is 1, that is, when the inputs are different.

A

B

A XOR B

0

0

0

0

1

1

1

0

1

1

1

0

XOR differs from OR only in the last row. A light controlled by two switches at opposite ends of a staircase is an XOR circuit: flipping either switch changes the state of the light.

Definition

XNOR gate: a logic gate whose output is 1 when its two inputs are equal (both 0 or both 1), equivalent to an XOR gate followed by a NOT gate.

A

B

A XNOR B

0

0

1

0

1

0

1

0

0

1

1

1

XNOR is an equality tester for single bits, which makes it the building block of comparators.

Common mistake

Confusing XOR with OR. OR gives 1 when at least one input is 1, including when both are; XOR gives 1 only when exactly one is 1, so 1 XOR 1 = 0. In everyday speech "or" is often exclusive ("tea or coffee?"), but the OR gate is not.

Tip

Memory aid for the seven gates: AND needs all 1s; OR needs any 1; XOR needs the inputs to differ; XNOR needs them to match. An N in front (NAND, NOR, XNOR) flips every output, which is what the bubble on the symbol means.

YouTube
Crash Course Computer Science, “Boolean Logic & Logic Gates”: how transistors combine into NOT, AND, OR and XOR gates, with the truth table and symbol for each.

Universal gates

NAND and NOR have a special property: each one on its own can produce every other gate. Joining both inputs of a NAND gate turns it into a NOT gate, because A⋅A‾=A‾\overline{A \cdot A} = \overline{A}A⋅A=A. Following a NAND with a NOT (made from another NAND) gives AND. Inverting both inputs and then applying NAND gives OR, by De Morgan's laws.

Definition

Universal gate: a logic gate from which any other logic gate, and therefore any logic circuit, can be built; NAND and NOR are the two universal gates.

NAND gates only. NOT A: one NAND gate with both inputs joined to A. A AND B: a NAND gate followed by a NAND gate with joined inputs. A OR B: two NAND gates with joined inputs invert A and B, and a third NAND gate combines them.
One NAND gives NOT, two give AND and three give OR, so a chip made of identical NAND gates can implement any circuit.

Manufacturers use this property because a chip that repeats one type of gate is simpler to design and make, and a NAND gate needs fewer transistors than an AND gate (an AND gate is a NAND followed by an inverter). NAND flash memory, used in SSDs and memory cards, takes its name from NAND for a related reason: its cells are wired in a NAND-like arrangement that packs them densely.

Applications of logic gates in computer systems

Logic gates are the physical form of every operation a processor performs. The main places they appear:

  • Arithmetic. The arithmetic logic unit (ALU) adds binary numbers with chains of adder circuits built from XOR, AND and OR gates, and carries out bitwise AND, OR, XOR and NOT on whole registers.

  • Comparison. An equality comparator applies XNOR to each pair of bits and ANDs the results: two numbers are equal only if every pair of bits matches. Comparisons decide the outcome of every condition in a program.

  • Memory. A latch, made from two cross-connected NAND or NOR gates, holds one bit for as long as power is supplied. Flip-flops built from latches make up registers, and the memory cells of a cache use the same cross-connected idea.

  • Control logic. The control unit decodes each instruction's opcode with AND and NOT gates so that exactly one operation is switched on, and selects which register or memory location is connected to the data bus.

  • Error detection. XOR-ing all the bits of a byte gives its parity bit: the result is 1 when the byte contains an odd number of 1s, so a single flipped bit during transmission changes the parity and can be detected.

  • Masking. AND with a mask keeps chosen bits and clears the rest, which is how a subnet mask extracts the network part of an IP address.

The half adder

Adding two single bits gives four cases: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 2, which is 10 in binary. The result needs two output bits: a sum bit (S) and a carry bit (C) passed to the next column.

A

B

C (carry)

S (sum)

0

0

0

0

0

1

0

1

1

0

0

1

1

1

1

0

The sum column is exactly the XOR truth table and the carry column is exactly the AND truth table, so the circuit needs just those two gates.

Definition

Half adder: a logic circuit that adds two single bits, producing a sum bit (A XOR B) and a carry bit (A AND B).

Half adder: inputs A and B both feed an XOR gate, whose output S is the sum bit, and an AND gate, whose output C is the carry bit.
Both inputs branch to two gates at once; the dots mark where a wire splits, so A and B each reach the XOR gate and the AND gate.

A half adder cannot accept a carry coming in from a previous column, so a full adder combines two half adders and an OR gate to add three bits. Chaining 64 full adders gives a circuit that adds two 64-bit numbers, the basic design of the ALU's adder (real processors add extra carry logic to make it faster). Programs can use the same operations through the bitwise operators ^ (XOR) and & (AND), which exist in both languages.

Python
print("A B | S C")
for a in [0, 1]:
    for b in [0, 1]:
        total = a ^ b
        carry = a & b
        print(a, b, "|", total, carry)
Java version
Java
public class Adder {
    public static void main(String[] args) {
        System.out.println("A B | S C");
        for (int a = 0; a <= 1; a++) {
            for (int b = 0; b <= 1; b++) {
                int total = a ^ b;
                int carry = a & b;
                System.out.println(a + " " + b + " | " + total + " " + carry);
            }
        }
    }
}
Plain text
A B | S C
0 0 | 0 0
0 1 | 1 0
1 0 | 1 0
1 1 | 0 1

Linking question: How might a conditional statement in a program be built from logic gates (subtopic B2.3, Programming constructs)? A condition such as age >= 16 and has_permission is a Boolean expression: the comparison is carried out by comparator circuits in the ALU, the result is combined with has_permission by an AND operation, and the single output bit is stored in a flag. A conditional jump instruction tests that flag, so the processor either skips the code in the selection statement or runs it.

Truth tables

Constructing a truth table

A truth table lists every combination of inputs exactly once. With nnn inputs there are 2n2^{n}2n combinations: 4 rows for two inputs, 8 for three and 16 for four. Writing the inputs in binary counting order (000, 001, 010, … , 111) guarantees that no row is missed or repeated: the rightmost input column alternates 0, 1, 0, 1, the next alternates in pairs, and the leftmost has all 0s in its top half and all 1s in its bottom half.

For any expression more complex than a single gate, add an intermediate column for each sub-expression, working from the innermost brackets outwards, and finish with the output column. Each intermediate column is filled using one gate's truth table, which turns a hard problem into several easy ones and leaves a record that can be checked. For Q=(A+B)⋅C‾Q = (A + B) \cdot \overline{C}Q=(A+B)⋅C:

A

B

C

A OR B

NOT C

Q

0

0

0

0

1

0

0

0

1

0

0

0

0

1

0

1

1

1

0

1

1

1

0

0

1

0

0

1

1

1

1

0

1

1

0

0

1

1

0

1

1

1

1

1

1

1

0

0

The output is 1 in three rows, those where at least one of A and B is 1 and C is 0.

Common mistake

Leaving rows out or listing them in a random order. A three-input table with seven rows, or with 101 written twice, cannot describe the circuit completely. Count the rows (2n2^{n}2n) and list the inputs in binary counting order every time.

Predicting the output of a logic circuit

To analyse a circuit, label the output of every gate that feeds another gate (P, R, …), give each label its own column, and work through the gates from the inputs towards the output. The circuit in the figure has three inputs and three gates.

Logic circuit with inputs A, B and C. A NAND gate takes A and B and gives P. An XOR gate takes B and C and gives R. An AND gate takes P and R and gives the output Q.
Input B branches (the dot) to two gates; labelling the intermediate wires P and R gives each gate its own column in the truth table.

Worked example

For the circuit with gates P = A NAND B, R = B XOR C and Q = P AND R:

(a) Construct the truth table, showing P and R. [4]

(b) State the input combinations for which Q = 1. [1]

Show the solution

(a) Fill P from the NAND table (0 only when A = B = 1), R from the XOR table (1 when B and C differ), then Q = 1 only where both P and R are 1.

A

B

C

P = A NAND B

R = B XOR C

Q = P AND R

0

0

0

1

0

0

0

0

1

1

1

1

0

1

0

1

1

1

0

1

1

1

0

0

1

0

0

1

0

0

1

0

1

1

1

1

1

1

0

0

1

0

1

1

1

0

0

0

(b) Q = 1 for ABC = 001, 010 and 101.

Check: the row 110 has R = 1 but P = 0, because A NAND B is 0 when both are 1, so Q = 0 there even though B and C differ.

Truth tables from a problem description

Many problems describe the required behaviour in words. The method is to name an input for every condition (1 = true), name the output, then decide the output for every row by reading the description, before writing any expression. Words translate into operators: "and", "both" and "as well as" into AND; "or", "either" and "at least one" into OR; "not", "unless" and "is off" into NOT; "exactly one" or "but not both" into XOR.

Worked example

A car has a warning buzzer Q. It sounds when the engine is running (E = 1) and the driver's seat belt is not fastened (S = 0), or when the engine is off (E = 0) and the headlights are on (H = 1).

(a) Construct the truth table for Q. [3]

(b) Construct a Boolean expression for Q. [2]

Show the solution

(a) Use intermediate columns for the two situations described.

E

S

H

E AND NOT S

NOT E AND H

Q

0

0

0

0

0

0

0

0

1

0

1

1

0

1

0

0

0

0

0

1

1

0

1

1

1

0

0

1

0

1

1

0

1

1

0

1

1

1

0

0

0

0

1

1

1

0

0

0

(b) The description gives the expression directly:

Q=E⋅S‾+E‾⋅HQ = E \cdot \overline{S} + \overline{E} \cdot HQ=E⋅S+E⋅H

In words: Q = (E AND NOT S) OR (NOT E AND H).

The same table can be produced by a program that loops through every input combination, in the same counting order, and evaluates the condition. This is also how the expression would appear as an if condition in software.

Python
print("E S H | Q")
for e in [False, True]:
    for s in [False, True]:
        for h in [False, True]:
            q = (e and not s) or (not e and h)
            print(int(e), int(s), int(h), "|", int(q))
Java version
Java
public class Buzzer {
    public static void main(String[] args) {
        boolean[] values = {false, true};
        System.out.println("E S H | Q");
        for (boolean e : values) {
            for (boolean s : values) {
                for (boolean h : values) {
                    boolean q = (e && !s) || (!e && h);
                    System.out.println(toBit(e) + " " + toBit(s) + " " + toBit(h) + " | " + toBit(q));
                }
            }
        }
    }

    static int toBit(boolean b) {
        if (b) {
            return 1;
        }
        return 0;
    }
}

The nested loops produce the rows in binary counting order, with the outer loop as the leftmost column. Python converts a Boolean to 0 or 1 with int(); Java has no such conversion, so toBit does it with an if.

Plain text
E S H | Q
0 0 0 | 0
0 0 1 | 1
0 1 0 | 0
0 1 1 | 1
1 0 0 | 1
1 0 1 | 1
1 1 0 | 0
1 1 1 | 0

From a truth table to a Boolean expression

Sometimes only the truth table is known, from a description or from testing a circuit. An expression can always be read from it: for each row where the output is 1, write an AND term that is true for that row only (each input as itself if it is 1 in that row, or inverted if it is 0), then OR all those terms together.

Definition

Sum of products: a form of Boolean expression made of AND terms (products) joined by OR (a sum).

A⋅B‾+A‾⋅BA \cdot \overline{B} + \overline{A} \cdot BA⋅B+A⋅B is a sum of products with two terms; it is the XOR function.

For the buzzer, the output is 1 in rows 001, 011, 100 and 101, so the sum of products is:

Q=E‾ S‾H+E‾SH+ES‾ H‾+ES‾HQ = \overline{E}\,\overline{S}H + \overline{E}SH + E\overline{S}\,\overline{H} + E\overline{S}HQ=ESH+ESH+ESH+ESH

This expression is correct but long: four three-input AND gates, a four-input OR gate and NOT gates for the inverted inputs. It describes the same behaviour as the two-term expression ES‾+E‾HE\overline{S} + \overline{E}HES+EH found from the description, which shows why simplification matters: a sum of products read from a table is rarely the smallest circuit. Boolean algebra and Karnaugh maps are the two tools for shrinking it.

Checking that two expressions are equivalent

Two expressions are equivalent when they give the same output for every combination of inputs. Building the truth table of both, side by side, settles the question: if the two output columns match in every row, the expressions are equivalent; a single differing row disproves it. This is the standard way to check any simplification.

A

B

NOT (A AND B)

NOT A OR NOT B

NOT A AND NOT B

0

0

1

1

1

0

1

1

1

0

1

0

1

1

0

1

1

0

0

0

The table proves that NAND equals A‾+B‾\overline{A} + \overline{B}A+B and is not the same as A‾⋅B‾\overline{A} \cdot \overline{B}A⋅B, which differs in two rows.

Common mistake

Treating NAND as "NOT A AND NOT B". The bar over A⋅B‾\overline{A \cdot B}A⋅B covers the whole AND, so it inverts the result of the AND, not each input. By De Morgan's laws A⋅B‾=A‾+B‾\overline{A \cdot B} = \overline{A} + \overline{B}A⋅B=A+B: the AND becomes an OR when the bar is split. Breaking the bar without changing the operator is the most common error in simplification.

Simplifying expressions with Boolean algebra

Why simplify

Every gate in a circuit costs chip area, power and a small delay as the signal passes through it. An expression that gives the same truth table with fewer operators can be built with fewer gates, so it is cheaper, uses less energy and produces its output sooner. Boolean algebra simplifies expressions by applying a small set of laws, each of which can be proved by a truth table. Most laws come in pairs: swapping AND with OR and 0 with 1 in one law gives its partner.

The laws of Boolean algebra

Definition

Identity law: the rule that ANDing a variable with 1, or ORing it with 0, leaves it unchanged: A⋅1=AA \cdot 1 = AA⋅1=A and A+0=AA + 0 = AA+0=A.

A⋅1=AA+0=AA \cdot 1 = A \qquad A + 0 = AA⋅1=AA+0=A

A 1 on one input of an AND gate lets the other input through unchanged, and so does a 0 on an OR gate. The opposite constants force the output, whatever the variable is.

Definition

Null law: the rule that ANDing a variable with 0 always gives 0, and ORing it with 1 always gives 1: A⋅0=0A \cdot 0 = 0A⋅0=0 and A+1=1A + 1 = 1A+1=1.

A⋅0=0A+1=1A \cdot 0 = 0 \qquad A + 1 = 1A⋅0=0A+1=1

Combining a variable with itself adds no information.

Definition

Idempotent law: the rule that ANDing or ORing a variable with itself gives the same variable: A⋅A=AA \cdot A = AA⋅A=A and A+A=AA + A = AA+A=A.

A⋅A=AA+A=AA \cdot A = A \qquad A + A = AA⋅A=AA+A=A

A variable and its inverse are always opposite, so exactly one of them is 1 at any moment.

Definition

Complement law: the rule that ANDing a variable with its inverse gives 0, and ORing a variable with its inverse gives 1: A⋅A‾=0A \cdot \overline{A} = 0A⋅A=0 and A+A‾=1A + \overline{A} = 1A+A=1.

A⋅A‾=0A+A‾=1A \cdot \overline{A} = 0 \qquad A + \overline{A} = 1A⋅A=0A+A=1

Inverting twice returns the original value, which is why two NOT gates in a row can be removed.

Definition

Double negation law: the rule that inverting a variable twice gives the original variable: A‾‾=A\overline{\overline{A}} = AA=A.

A‾‾=A\overline{\overline{A}} = AA=A

The order of the inputs to an AND or OR gate does not matter.

Definition

Commutative law: the rule that the order of the operands of AND or OR does not change the result: A⋅B=B⋅AA \cdot B = B \cdot AA⋅B=B⋅A and A+B=B+AA + B = B + AA+B=B+A.

A⋅B=B⋅AA+B=B+AA \cdot B = B \cdot A \qquad A + B = B + AA⋅B=B⋅AA+B=B+A

Nor does the grouping, when every operator in a chain is the same, which is why a three-input AND gate needs no brackets.

Definition

Associative law: the rule that the grouping of three or more operands joined by the same operator does not change the result: (A⋅B)⋅C=A⋅(B⋅C)(A \cdot B) \cdot C = A \cdot (B \cdot C)(A⋅B)⋅C=A⋅(B⋅C) and (A+B)+C=A+(B+C)(A + B) + C = A + (B + C)(A+B)+C=A+(B+C).

(A⋅B)⋅C=A⋅(B⋅C)(A+B)+C=A+(B+C)(A \cdot B) \cdot C = A \cdot (B \cdot C) \qquad (A + B) + C = A + (B + C)(A⋅B)⋅C=A⋅(B⋅C)(A+B)+C=A+(B+C)

AND distributes over OR exactly as multiplication distributes over addition, which allows a common factor to be taken out of two terms. Unlike ordinary arithmetic, OR also distributes over AND.

Definition

Distributive law: the rule that an operator applied to a bracket can be applied to each term inside it: A⋅(B+C)=A⋅B+A⋅CA \cdot (B + C) = A \cdot B + A \cdot CA⋅(B+C)=A⋅B+A⋅C and A+B⋅C=(A+B)⋅(A+C)A + B \cdot C = (A + B) \cdot (A + C)A+B⋅C=(A+B)⋅(A+C).

A⋅(B+C)=A⋅B+A⋅CA+B⋅C=(A+B)⋅(A+C)A \cdot (B + C) = A \cdot B + A \cdot C \qquad A + B \cdot C = (A + B) \cdot (A + C)A⋅(B+C)=A⋅B+A⋅CA+B⋅C=(A+B)⋅(A+C)

A term that contains another term in full is redundant beside it. In A+A⋅BA + A \cdot BA+A⋅B, whenever A⋅BA \cdot BA⋅B is 1, A is already 1, so the second term never changes the result.

Definition

Absorption law: the rule that a term containing a variable is redundant when it is combined with that variable on its own: A+A⋅B=AA + A \cdot B = AA+A⋅B=A and A⋅(A+B)=AA \cdot (A + B) = AA⋅(A+B)=A.

A+A⋅B=AA⋅(A+B)=AA + A \cdot B = A \qquad A \cdot (A + B) = AA+A⋅B=AA⋅(A+B)=A

The absorption law can itself be proved from the earlier laws, which is a good model for the step-by-step style of simplification:

A+A⋅B=A⋅1+A⋅Bidentity=A⋅(1+B)distributive=A⋅1null (1+B=1)=Aidentity\begin{aligned} A + A \cdot B &= A \cdot 1 + A \cdot B && \text{identity} \\ &= A \cdot (1 + B) && \text{distributive} \\ &= A \cdot 1 && \text{null } (1 + B = 1) \\ &= A && \text{identity} \end{aligned}A+A⋅B​=A⋅1+A⋅B=A⋅(1+B)=A⋅1=A​​identitydistributivenull (1+B=1)identity​

De Morgan's laws

The last pair of laws shows how to remove a bar that covers a whole expression. The bar is split over each variable and the operator underneath changes, AND to OR or OR to AND.

Definition

De Morgan's laws: the two rules A⋅B‾=A‾+B‾\overline{A \cdot B} = \overline{A} + \overline{B}A⋅B=A+B and A+B‾=A‾⋅B‾\overline{A + B} = \overline{A} \cdot \overline{B}A+B​=A⋅B, which state that the inverse of an AND is the OR of the inverses, and the inverse of an OR is the AND of the inverses.

A⋅B‾=A‾+B‾A+B‾=A‾⋅B‾\overline{A \cdot B} = \overline{A} + \overline{B} \qquad \overline{A + B} = \overline{A} \cdot \overline{B}A⋅B=A+BA+B​=A⋅B

In words: "it is not the case that both are true" means "at least one is false", and "it is not the case that either is true" means "both are false". The laws extend to any number of variables, A+B+C‾=A‾⋅B‾⋅C‾\overline{A + B + C} = \overline{A} \cdot \overline{B} \cdot \overline{C}A+B+C​=A⋅B⋅C, and they are the reason NAND and NOR can stand in for OR and AND. The phrase "break the bar, change the sign" is a common memory aid.

Law

AND form

OR form

Identity

A⋅1=AA \cdot 1 = AA⋅1=A

A+0=AA + 0 = AA+0=A

Null

A⋅0=0A \cdot 0 = 0A⋅0=0

A+1=1A + 1 = 1A+1=1

Idempotent

A⋅A=AA \cdot A = AA⋅A=A

A+A=AA + A = AA+A=A

Complement

A⋅A‾=0A \cdot \overline{A} = 0A⋅A=0

A+A‾=1A + \overline{A} = 1A+A=1

Double negation

A‾‾=A\overline{\overline{A}} = AA=A

Commutative

A⋅B=B⋅AA \cdot B = B \cdot AA⋅B=B⋅A

A+B=B+AA + B = B + AA+B=B+A

Associative

(AB)C=A(BC)(AB)C = A(BC)(AB)C=A(BC)

(A+B)+C=A+(B+C)(A + B) + C = A + (B + C)(A+B)+C=A+(B+C)

Distributive

A(B+C)=AB+ACA(B + C) = AB + ACA(B+C)=AB+AC

A+BC=(A+B)(A+C)A + BC = (A + B)(A + C)A+BC=(A+B)(A+C)

Absorption

A(A+B)=AA(A + B) = AA(A+B)=A

A+AB=AA + AB = AA+AB=A

De Morgan's

AB‾=A‾+B‾\overline{AB} = \overline{A} + \overline{B}AB=A+B

A+B‾=A‾ B‾\overline{A + B} = \overline{A}\,\overline{B}A+B​=AB

Two more results follow from these laws and appear so often that they save time when recognised: A⋅B+A⋅B‾=AA \cdot B + A \cdot \overline{B} = AA⋅B+A⋅B=A (factor out A, then complement and identity) and A+A‾⋅B=A+BA + \overline{A} \cdot B = A + BA+A⋅B=A+B (distributive law in its OR form, then complement and identity).

Simplifying step by step

A reliable order of attack: first remove bars over whole expressions with De Morgan's laws and double negation; then multiply out brackets if needed; then look for common factors, complements (X+X‾X + \overline{X}X+X) and absorbed terms. Name the law at every step, and check the final answer with a truth table.

Worked example

Simplify Q=A⋅B⋅C+A⋅B⋅C‾+A‾⋅B⋅CQ = A \cdot B \cdot C + A \cdot B \cdot \overline{C} + \overline{A} \cdot B \cdot CQ=A⋅B⋅C+A⋅B⋅C+A⋅B⋅C, naming the law used at each step. [4]

Show the solution
Q=ABC+ABC‾+A‾BC=AB(C+C‾)+A‾BCdistributive (factor out AB)=AB⋅1+A‾BCcomplement=AB+A‾BCidentity=B(A+A‾C)distributive (factor out B)=B((A+A‾)(A+C))distributive (OR form)=B(1⋅(A+C))complement=B(A+C)identity\begin{aligned} Q &= AB C + AB\overline{C} + \overline{A}BC \\ &= AB(C + \overline{C}) + \overline{A}BC && \text{distributive (factor out } AB) \\ &= AB \cdot 1 + \overline{A}BC && \text{complement} \\ &= AB + \overline{A}BC && \text{identity} \\ &= B(A + \overline{A}C) && \text{distributive (factor out } B) \\ &= B\big((A + \overline{A})(A + C)\big) && \text{distributive (OR form)} \\ &= B\big(1 \cdot (A + C)\big) && \text{complement} \\ &= B(A + C) && \text{identity} \end{aligned}Q​=ABC+ABC+ABC=AB(C+C)+ABC=AB⋅1+ABC=AB+ABC=B(A+AC)=B((A+A)(A+C))=B(1⋅(A+C))=B(A+C)​​distributive (factor out AB)complementidentitydistributive (factor out B)distributive (OR form)complementidentity​

Q = B · (A + C), in words B AND (A OR C): two gates instead of three three-input AND gates, an OR gate and two NOT gates.

Check with a truth table: the original is 1 for ABC = 011, 110 and 111; B(A+C)B(A + C)B(A+C) is 1 exactly when B = 1 and at least one of A and C is 1, which is the same three rows.

Exam tip

When asked to simplify with Boolean algebra, write one step per line and name the law on each line. Method marks are usually given for correct steps, so a long but well-labelled answer can still earn most of the marks if the final expression has a slip. End with the simplified expression stated clearly.

Simplifying a circuit

Algebra is often applied to an expression read from an existing circuit. The figure shows a five-gate circuit and the two-gate circuit that replaces it after simplification.

Before: NOT A and NOT B feed a NAND gate; A and C feed an OR gate; an AND gate combines the two to give Q, five gates. After simplification: B and C feed an AND gate, whose output is ORed with A to give Q, two gates.
A NAND gate fed by two inverted inputs is just an OR gate (De Morgan's law), and the remaining expression factorises, so the two circuits have identical truth tables with three fewer gates.

Worked example

Simplify Q=A‾⋅B‾‾⋅(A+C)Q = \overline{\overline{A} \cdot \overline{B}} \cdot (A + C)Q=A⋅B⋅(A+C), the expression for the five-gate circuit, and state how many gates the simplified circuit needs. [4]

Show the solution
Q=A‾⋅B‾‾⋅(A+C)=(A‾‾+B‾‾)⋅(A+C)De Morgan’s=(A+B)⋅(A+C)double negation=A+B⋅Cdistributive (OR form)\begin{aligned} Q &= \overline{\overline{A} \cdot \overline{B}} \cdot (A + C) \\ &= (\overline{\overline{A}} + \overline{\overline{B}}) \cdot (A + C) && \text{De Morgan's} \\ &= (A + B) \cdot (A + C) && \text{double negation} \\ &= A + B \cdot C && \text{distributive (OR form)} \end{aligned}Q​=A⋅B⋅(A+C)=(A+B)⋅(A+C)=(A+B)⋅(A+C)=A+B⋅C​​De Morgan’sdouble negationdistributive (OR form)​

Q = A + B · C, which needs two gates: one AND gate for B · C and one OR gate.

Check: both expressions give Q = 1 for ABC = 011, 100, 101, 110 and 111, and 0 for 000, 001 and 010.

Karnaugh maps

Gray code and adjacency

Algebraic simplification depends on spotting which terms to combine, and it is easy to miss one. A Karnaugh map makes the combinations visible. It is a truth table rearranged as a grid in which neighbouring cells differ in exactly one input, so any two neighbouring 1s share all their inputs but one, and that one input can be removed, just as AB+AB‾=AAB + A\overline{B} = AAB+AB=A.

Definition

Gray code: an ordering of binary values in which each value differs from the next in exactly one bit.

For two bits the Gray-code order is 00, 01, 11, 10.

The Gray-code order also wraps around: the last value, 10, differs from the first, 00, in one bit. So the left and right edges of a Karnaugh map are neighbours, as are the top and bottom edges, as if the map were drawn on a tube.

Definition

Karnaugh map: a grid form of a truth table, with rows and columns labelled in Gray-code order so that adjacent cells differ in one input, used to simplify Boolean expressions by grouping adjacent 1s.

Common mistake

Labelling Karnaugh map axes in counting order (00, 01, 10, 11). Cells 01 and 10 are then side by side although they differ in two bits, so grouping them gives a wrong term, and true neighbours such as 01 and 11 are split. Axes must follow Gray code: 00, 01, 11, 10.

Grouping the 1s

The method has four steps:

  1. Draw the grid: 2 × 2 for two inputs, 2 × 4 for three, 4 × 4 for four. Label rows and columns in Gray-code order (00, 01, 11, 10), never in counting order.

  2. Copy the output for each input combination into its cell.

  3. Loop all the 1s in rectangular groups of 1, 2, 4, 8 or 16 cells (powers of 2 only). Make each group as large as possible, use as few groups as possible, and allow groups to overlap and to wrap around the edges. Every 1 must be in at least one group; no 0 may be inside a group.

  4. For each group, write an AND term of the inputs that stay constant across the group (as the variable if it stays 1, inverted if it stays 0), dropping any input that changes. OR the terms together.

A group of two removes one variable, a group of four removes two and a group of eight removes three. Larger groups therefore give shorter terms, which is the reason for making each group as large as possible.

Two Karnaugh maps. Two variables, A by B: 1s at AB = 01, 10, 11; a loop on row A = 1 and a dashed loop on column B = 1 give Q = A OR B. Three variables, A by BC in order 00, 01, 11, 10: 1s in columns 00 and 10 form one wrap-around group, Q = NOT C.
Groups may overlap, as in the two-variable map; in the three-variable map the columns 00 and 10 are neighbours across the edge, so the four 1s form one group in which only C stays constant (at 0).

In the two-variable map, the expression A‾B+AB+AB‾\overline{A}B + AB + A\overline{B}AB+AB+AB has three 1s. The row A = 1 is one group (B changes, so the term is A) and the column B = 1 is another (A changes, so the term is B). The 1 at AB = 11 is used twice, which is allowed, and the result is Q=A+BQ = A + BQ=A+B. In the three-variable map, the 1s are at ABC = 000, 010, 100 and 110; across the group A and B both change and only C stays at 0, so the four terms reduce to C‾\overline{C}C.

Worked example

Use a Karnaugh map to simplify the car buzzer expression Q=E‾ S‾H+E‾SH+ES‾ H‾+ES‾HQ = \overline{E}\,\overline{S}H + \overline{E}SH + E\overline{S}\,\overline{H} + E\overline{S}HQ=ESH+ESH+ESH+ESH. [3]

Show the solution

Rows are E; columns are SH in Gray-code order.

E \ SH

00

01

11

10

0

0

1

1

0

1

1

1

0

0

  • Group 1: the two 1s in row E = 0, columns 01 and 11. E = 0 and H = 1 stay constant; S changes. Term: E‾H\overline{E}HEH.

  • Group 2: the two 1s in row E = 1, columns 00 and 01. E = 1 and S = 0 stay constant; H changes. Term: ES‾E\overline{S}ES.

Q=ES‾+E‾HQ = E\overline{S} + \overline{E}HQ=ES+EH

The 1s at E = 0, SH = 01 and E = 1, SH = 01 could also be grouped vertically (term S‾H\overline{S}HSH), but both are already covered, so that group would add a redundant term. The result matches the expression written from the description.

Four-variable maps and wrap-around

With four inputs the map is 4 × 4, with AB on the rows and CD on the columns, both in Gray-code order. Wrap-around now works in both directions, so the four corner cells are all neighbours of each other and can form a single group of four.

Four-variable Karnaugh map, rows AB and columns CD both in order 00, 01, 11, 10. 1s in the four corner cells form one group (NOT B AND NOT D); 1s in the central 2 by 2 square form another (B AND D). Q = (NOT B AND NOT D) OR (B AND D).
The four corners form one group because the map wraps top to bottom and left to right; in that group A and C both change while B and D stay at 0.

The corners are ABCD = 0000, 0010, 1000 and 1010: A and C change, while B = 0 and D = 0 throughout, so the term is B‾ D‾\overline{B}\,\overline{D}BD. The central square is 0101, 0111, 1101 and 1111: B = 1 and D = 1 throughout, so the term is BDBDBD. Eight minterms reduce to Q=B‾ D‾+BDQ = \overline{B}\,\overline{D} + BDQ=BD+BD, which is B XNOR D.

Worked example

A circuit with inputs A, B, C and D outputs 1 for exactly these inputs: ABCD = 0000, 0001, 0010, 0011, 1000, 1010, 1101 and 1111. Construct the Karnaugh map and simplify the output expression. [5]

Show the solution

Place each 1 in its cell (rows AB, columns CD, both 00, 01, 11, 10).

AB \ CD

00

01

11

10

00

1

1

1

1

01

0

0

0

0

11

0

1

1

0

10

1

0

0

1

  • Group 1: the whole top row (AB = 00), four cells. A = 0 and B = 0 are constant; C and D change. Term: A‾ B‾\overline{A}\,\overline{B}AB.

  • Group 2: the four corners, 0000, 0010, 1000 and 1010 (wrap-around top to bottom and left to right). B = 0 and D = 0 are constant. Term: B‾ D‾\overline{B}\,\overline{D}BD. Two of these cells are shared with group 1, which is allowed.

  • Group 3: 1101 and 1111, a pair in row AB = 11. A = 1, B = 1 and D = 1 are constant; C changes. Term: ABDABDABD.

No group of four can include 1101 and 1111 without also covering a 0, so a pair is the largest possible group for them.

Q=A‾ B‾+B‾ D‾+ABDQ = \overline{A}\,\overline{B} + \overline{B}\,\overline{D} + ABDQ=AB+BD+ABD

Check: this expression is 1 for exactly the eight listed inputs (a truth table of all 16 rows confirms it).

Common mistake

Making groups of three or six, or leaving out the wrap-around. Group sizes must be powers of 2 (1, 2, 4, 8, 16) because each doubling removes exactly one variable; a group of three removes nothing cleanly. Check the edges and corners last: 1s in the first and last column (or row) are neighbours.

Karnaugh maps or Boolean algebra

The two methods reach the same answer, but each suits different situations.

Boolean algebra

Karnaugh map

Works from

any expression, including ones with bars over brackets

a truth table or a sum of products

Number of inputs

any

practical for 2 to 4 (5 and 6 need several maps)

Finding the simplest form

relies on spotting which law to apply; easy to stop too early

grouping shows the simplest sum of products directly

Showing the reasoning

each step is named and checkable

the groups are the reasoning

Typical use

proving two expressions are equal; removing NAND/NOR structures

minimising an output given as a table

In practice the methods are combined: De Morgan's laws first remove bars over brackets, then a Karnaugh map finds the minimal sum of products, and a truth table confirms the result. Circuit design software does the same job automatically for thousands of inputs with algorithms based on the same idea of combining terms that differ in one variable.

Logic diagrams

Reading and drawing logic diagrams

A circuit is shown on paper as a logic diagram. The conventions are the same whether the diagram is read or drawn: inputs on the left, labelled with their variable names; signals flowing left to right through the gates; and the output on the right, labelled Q or with its own name.

Definition

Logic diagram: a diagram of a logic circuit that uses standard gate symbols connected by lines (wires) to show how the inputs are processed to produce the outputs.

  • A dot where wires meet shows a junction: the signal splits and goes both ways.

  • Wires that cross without a dot are not connected.

  • Each gate output may feed several inputs, but two gate outputs must never be joined directly; they must meet at a gate.

  • Gates can have more than two inputs: a three-input AND gate gives A⋅B⋅CA \cdot B \cdot CA⋅B⋅C directly.

To construct a diagram from an expression, follow the order of evaluation: draw a NOT gate for each inverted input, then the AND gates for each product term, then an OR gate combining them, working outwards from the innermost brackets. To derive an expression from a diagram, do the reverse: label each gate's output with its expression, starting next to the inputs, until the output is reached.

Worked example

Construct a logic diagram for the car buzzer, Q=E⋅S‾+E‾⋅HQ = E \cdot \overline{S} + \overline{E} \cdot HQ=E⋅S+E⋅H, using AND, OR and NOT gates. [3]

Show the solution

Work from the inputs to the output:

  1. NOT gates for the inverted inputs: one producing S‾\overline{S}S and one producing E‾\overline{E}E. E is also needed uninverted, so its wire splits at a junction.

  2. One AND gate for each product: E⋅S‾E \cdot \overline{S}E⋅S and E‾⋅H\overline{E} \cdot HE⋅H.

  3. One OR gate joining the two AND outputs to give Q.

Logic circuit with inputs E, S and H. S passes through a NOT gate and E through another NOT gate. One AND gate combines E and NOT S; a second combines NOT E and H. An OR gate combines the two AND outputs to give Q.
The E wire splits at the junction dot: one branch goes straight to the upper AND gate and the other through a NOT gate to the lower one; the S wire crosses that branch without a dot, so the two are not connected.

The diagram uses five gates: two NOT, two AND and one OR. One mark is typically given for each correct part: the NOT gates, the AND gates with the right inputs, and the OR gate producing Q.

Exam tip

In a construct question for a logic diagram, use the distinctive-shape symbols, label every input and the output, and mark junctions with a dot. Draw only the gates the question allows: if it says "using only NAND gates", every gate in the answer must be a NAND gate. Ambiguous symbols (an AND that could be an OR) are not credited, so draw the curved backs of OR, NOR, XOR and XNOR gates clearly.

Building complex operations from simple gates

Larger operations are combinations of the seven gates. XOR itself can be built from AND, OR and NOT, since its truth table gives the sum of products A⊕B=AB‾+A‾BA \oplus B = A\overline{B} + \overline{A}BA⊕B=AB+AB, and XNOR is AB+A‾ B‾AB + \overline{A}\,\overline{B}AB+AB. An equality comparator for two 4-bit numbers uses four XNOR gates (one per bit pair) feeding a four-input AND gate. A majority voter for three sensors, whose output follows at least two of them, is AB+AC+BCAB + AC + BCAB+AC+BC: three AND gates and an OR gate. In each case the design follows the same chain: a description in words, a truth table, an expression, a simplification, and finally the diagram.

Computational thinking: Circuit design is a clear case of abstraction and decomposition. A half adder is built from gates; once it works, it is treated as a single block with inputs A, B and outputs S, C, and full adders, 64-bit adders and the ALU are built from those blocks without looking inside them again. A word problem is decomposed in the same way, into one input per condition and one intermediate column per sub-expression.

Tip

Improve your notes: Make a one-page reference sheet with the seven gate symbols, each drawn by hand beside its truth table and its expression in both word and symbol form. Underneath, add the table of Boolean laws, then take an expression with at least three terms, simplify it, and record each law you used, with the truth table check beside it.

Exam focus

Data representation and computer logic are examined in Paper 1, Section A, in structured questions that usually set a short scenario and then ask several parts of rising difficulty. Typical shapes are:

  • Calculate or convert: a binary, denary or hexadecimal value, with working (1–2 marks each part); the largest value or the number of values for a given number of bits (1 mark); the size of an image, sound or video file (2–3 marks).

  • Describe or explain: how a character, image or sound is stored; why hexadecimal is used; the effect of colour depth or sampling rate on quality and file size (2–4 marks).

  • Construct: a truth table from a circuit, an expression or a description (3–4 marks); a logic diagram from an expression or a description (3 marks); a simplified expression using Boolean algebra or a Karnaugh map (3–5 marks).

  • Describe the function of a named gate, or identify the gate from its truth table (1–2 marks).

"Construct" asks for information in diagrammatic or logical form, so the marks go to complete, correct tables and diagrams. "Analyse" in the context of truth tables means deducing the output, or the expression, from the information given. Calculations earn marks for method, so working must be shown, and the final answer needs a unit.

Exam tip

A greenhouse controller stores the latest temperature reading in an 8-bit register as an unsigned integer.

(a) The register holds 1100 1011.

(i) State its denary value. [1]

(ii) Calculate its hexadecimal representation. [1]

(b) State the largest value the register can hold. [1]

The roof vent V opens (V = 1) when the temperature is high (T = 1) and either the humidity is high (H = 1) or it is not raining (R = 0).

(c) Construct the truth table for V. [3]

(d) Construct a logic diagram for V using AND, OR and NOT gates. [3]

(e) An engineer writes the expression V=T⋅H⋅R+T⋅H⋅R‾+T⋅H‾⋅R‾V = T \cdot H \cdot R + T \cdot H \cdot \overline{R} + T \cdot \overline{H} \cdot \overline{R}V=T⋅H⋅R+T⋅H⋅R+T⋅H⋅R. Using Boolean algebra, show that it simplifies to T⋅(H+R‾)T \cdot (H + \overline{R})T⋅(H+R). [3]

Show the markscheme

(a)(i) 203 [1]

(a)(ii) CB [1]. Accept cb or 0xCB.

(b) 255 [1]. Do not accept 256.

(c) Award marks as follows, up to [3]:

  • eight rows in a systematic order (all combinations of T, H and R) [1];

  • V = 0 for every row with T = 0 [1];

  • V = 1 for THR = 100, 110 and 111, and V = 0 for THR = 101 [1].

T

H

R

V

0

0

0

0

0

0

1

0

0

1

0

0

0

1

1

0

1

0

0

1

1

0

1

0

1

1

0

1

1

1

1

1

Award intermediate columns (NOT R, H OR NOT R) as working; they are not required.

(d) Award [1] for each, up to [3]:

  • a NOT gate with input R;

  • an OR gate with inputs H and NOT R;

  • an AND gate with inputs T and the OR output, producing V.

Accept an equivalent correct circuit. Inputs and output must be labelled.

(e) Award [1] for each step, up to [3]:

  • TH(R+R‾)+TH‾ R‾TH(R + \overline{R}) + T\overline{H}\,\overline{R}TH(R+R)+THR (distributive), giving TH+TH‾ R‾TH + T\overline{H}\,\overline{R}TH+THR (complement, identity);

  • T(H+H‾ R‾)T(H + \overline{H}\,\overline{R})T(H+HR) (distributive);

  • T(H+R‾)T(H + \overline{R})T(H+R), since H+H‾ R‾=H+R‾H + \overline{H}\,\overline{R} = H + \overline{R}H+HR=H+R.

Accept a correct chain using other laws, or a full truth table showing both expressions agree in all eight rows for [2] max.

Total [13]

The commonest losses on data representation and logic come from small slips rather than misunderstanding, so a quick check at the end of each part pays off.

Exam tip

For conversions, always show the method: place values with the bits beneath them, the divisions with remainders, or the groups of four. A bare answer that is wrong earns nothing, but a correct method with one slip can still earn a mark. Convert the answer back to check it whenever time allows.

In file size calculations, write the formula first, substitute the values and give the unit at every stage. If the question does not say whether 1000 or 1024 is expected, state which you used.

Exam tip

In truth table questions, count the rows before filling them in (2n2^{n}2n for nnn inputs), use counting order, and add a column for every intermediate gate output. A missing row or a wrong row order is an easy way to lose a whole mark, and intermediate columns let partial credit be given for a later slip.

Practice questions

  1. State the number of different values that can be represented with 12 bits, and the largest unsigned integer that 12 bits can hold. [2]

  2. Calculate the denary and hexadecimal values of the binary number 0110 1011. [2]

  3. Calculate the binary and denary values of the hexadecimal number E7. [2]

  4. Explain why the integer 255 and the string "255" need different amounts of storage. [2]

  5. A camera takes photos with a resolution of 4000 × 3000 pixels and a colour depth of 24 bits. Calculate the uncompressed size of one photo in MB. [2]

  6. A podcast is recorded in stereo at a sampling rate of 48,000 Hz with a bit depth of 24 bits. Calculate the uncompressed size of a 3-minute recording in MB. [3]

  7. Describe the function of an XOR gate and identify one use of XOR in a computer system. [2]

  8. Construct the truth table for Q=A+B‾⊕CQ = \overline{A + B} \oplus CQ=A+B​⊕C. [3]

  9. Simplify Q=A⋅B+A⋅B‾+A‾⋅BQ = A \cdot B + A \cdot \overline{B} + \overline{A} \cdot BQ=A⋅B+A⋅B+A⋅B using Boolean algebra, naming each law. [3]

  10. A circuit with inputs A, B, C and D outputs 1 for exactly these inputs: ABCD = 0000, 0010, 0011, 0111, 1000, 1010, 1011 and 1111. Construct a Karnaugh map and simplify the output expression. [4]

Answers
  1. 212=40962^{12} = 4096212=4096 values [1]; largest 4095 [1].

  2. 64+32+8+2+1=10764 + 32 + 8 + 2 + 1 = 10764+32+8+2+1=107 [1]; 0110 = 6, 1011 = B, so 6B [1].

  3. E = 1110, 7 = 0111, so 1110 0111 [1]; 14×16+7=23114 \times 16 + 7 = 23114×16+7=231 [1].

  4. The integer is stored as one binary number, 1111 1111, which fits in one byte [1]; the string is three characters, each stored as its own character code (50, 53, 53), so it needs three bytes in ASCII or UTF-8 [1].

  5. 4000×3000×24=288,000,0004000 \times 3000 \times 24 = 288{,}000{,}0004000×3000×24=288,000,000 bits [1]; ÷8=36,000,000\div 8 = 36{,}000{,}000÷8=36,000,000 bytes = 36 MB [1].

  6. 48,000×24×2×180=414,720,00048{,}000 \times 24 \times 2 \times 180 = 414{,}720{,}00048,000×24×2×180=414,720,000 bits [1]; ÷8=51,840,000\div 8 = 51{,}840{,}000÷8=51,840,000 bytes [1]; about 51.8 MB [1] (or about 49.4 MiB).

  7. The output is 1 when exactly one of the two inputs is 1 (the inputs differ), and 0 when they are equal [1]. Uses: the sum bit of a half adder / generating a parity bit / comparing bits for difference [1].

  8. Rows ABC from 000 to 111, with A+B‾\overline{A + B}A+B​ = 1, 1, 0, 0, 0, 0, 0, 0 and Q = 1, 0, 0, 1, 0, 1, 0, 1. Award [1] for eight rows in order, [1] for the NOR column, [1] for the Q column.

  9. AB+AB‾=A(B+B‾)AB + A\overline{B} = A(B + \overline{B})AB+AB=A(B+B) (distributive) =A⋅1=A= A \cdot 1 = A=A⋅1=A (complement, identity) [1]; Q=A+A‾BQ = A + \overline{A}BQ=A+AB [1]; =(A+A‾)(A+B)=A+B= (A + \overline{A})(A + B) = A + B=(A+A)(A+B)=A+B (distributive, complement, identity) [1].

  10. Map with rows AB and columns CD in the order 00, 01, 11, 10 [1]; the whole column CD = 11 is a group of four, giving CDCDCD [1]; the four corners (0000, 0010, 1000, 1010) are a wrap-around group of four, giving B‾ D‾\overline{B}\,\overline{D}BD [1]; Q=CD+B‾ D‾Q = CD + \overline{B}\,\overline{D}Q=CD+BD [1].

Summary

  • A bit is one binary digit; a nibble is 4 bits (one hex digit) and a byte is 8 bits. With nnn bits there are 2n2^{n}2n patterns, and an unsigned integer ranges from 0 to 2n−12^{n} - 12n−1.

  • Binary, denary and hexadecimal are place-value systems with bases 2, 10 and 16. Convert to denary by adding digit × place value; convert from denary by repeated division (remainders read upwards); convert binary to hex by grouping bits in fours from the right.

  • Overflow happens when a result needs more bits than are available; the extra bit is lost and the stored value wraps round.

  • Text is stored as character codes: ASCII uses 7 bits for 128 characters; Unicode covers every writing system, and UTF-8 encodes each code point in 1 to 4 bytes, ASCII in one.

  • A bitmap stores a colour value for every pixel: size in bits = width × height × colour depth. Vector graphics store shapes and scale without losing quality.

  • Sound is sampled by an ADC: size in bits = sampling rate × bit depth × channels × seconds; a DAC turns samples back into sound. Video is frames × frame rate plus audio, almost always compressed.

  • More bits or more samples give higher fidelity but proportionally larger files; data can only be read back with the same encoding it was written with.

  • Logic gates (AND, OR, NOT, NAND, NOR, XOR, XNOR) are transistor circuits that apply Boolean operations; NAND and NOR are universal. Gates build adders (a half adder is XOR + AND), comparators, memory and control logic.

  • A truth table lists all 2n2^{n}2n input rows in counting order; a sum of products is read from the rows with output 1. Boolean algebra laws (including De Morgan's) and Karnaugh maps with Gray-code axes and power-of-2 groups simplify expressions, and a truth table checks the result.

  • Logic diagrams use standard symbols, flow left to right, mark junctions with dots, and are built from the inside of an expression outwards.

Self-check

  • To do:

    I can write the place values of an 8-bit binary number and of a 3-digit hexadecimal number.

  • To do:

    I can convert any integer up to 16 bits between binary, denary and hexadecimal in both directions, with working.

  • To do:

    I can group bits in fours from the right, padding on the left, to convert binary to hexadecimal.

  • To do:

    I can state the range of an nnn-bit unsigned integer and work out the bits needed for a given number of values.

  • To do:

    I can explain overflow with an 8-bit example and describe the Year 2038 problem.

  • To do:

    I can tell decimal prefixes (kB, MB) from binary prefixes (KiB, MiB) and convert bits to bytes.

  • To do:

    I can explain how ASCII, Unicode and UTF-8 store characters and calculate the bytes a short string takes in UTF-8.

  • To do:

    I can calculate the size of a bitmap image, an audio recording and an uncompressed video.

  • To do:

    I can explain how sampling rate, bit depth, resolution and colour depth affect quality and file size.

  • To do:

    I can draw the symbol and write the truth table of each of the seven gates.

  • To do:

    I can construct a truth table from a circuit, an expression or a description, with intermediate columns.

  • To do:

    I can write a sum-of-products expression from a truth table.

  • To do:

    I can simplify an expression with the Boolean laws, naming each law, and apply De Morgan's laws correctly.

  • To do:

    I can simplify an expression with a 2-, 3- or 4-variable Karnaugh map, including wrap-around groups.

  • To do:

    I can construct a logic diagram from an expression or description, and derive the expression from a diagram.

Questions on A1.2

95 exam-style questions on this subtopic, written in the style of the IB papers. The first has its markscheme open. Counted on 20 October 2026

Question 1 · CS-000715 marks

A coastal weather station measures wind speed and humidity. Each reading is stored as an 8-bit unsigned binary integer before it is sent to a central server.

  1. (a)

    The latest wind speed reading is 1011 0110. State this value in decimal.

    [1]1 mark

    Markscheme (a)

    182;

  2. (b)

    The latest humidity reading is 203 in decimal. State this value in hexadecimal.

    [1]1 mark

    Markscheme (b)

    CB;

    Note: Accept CB16CB_{16}CB16​, 0xCB or lower case cb.

  3. (c)

    State the largest value that one reading can hold.

    [1]1 mark

    Markscheme (c)

    255;

    Note: Do not accept 256.

  4. (d)

    Outline one reason why the station's engineers display readings in hexadecimal rather than binary when testing the equipment.

    [2]2 marks

    Markscheme (d)

    Award [1] for the reason and [1] for the development, up to [2 max].
    Hexadecimal is shorter / more compact (two digits instead of eight);
    so it is quicker to read and copy, with fewer transcription errors;
    Each hex digit maps to exactly four bits;
    so it converts to and from binary easily (unlike decimal);

    Note: Do not accept "hexadecimal uses less memory": the stored data is still binary.

Question 2 · CS-0007211 marks

A web design studio stores each colour on its clients' websites as a 24-bit RGB value: one byte each for red, green and blue. Colours are written as six hexadecimal digits; for example, the studio's sea green is #2E8B57, where 2E is red, 8B is green and 57 is blue.

  1. (a)

    State the number of bits used to store the red component.

    [1]1 mark

  2. (b)

    Convert the green component 8B to decimal, showing your working.

    [2]2 marks

  3. (c)

    Convert the blue component 57 to 8-bit binary.

    [1]1 mark

  4. (d)

    Calculate how many different colours can be represented with this 24-bit scheme.

    [2]2 marks

  5. (e)

    A client wants a colour with red = 200, green = 16 and blue = 255 (all decimal). Construct the six-digit hexadecimal code for this colour.

    [2]2 marks

  6. (f)

    Explain why the studio writes colours in hexadecimal rather than in binary or decimal.

    [3]3 marks

Answer it, then compare with the markscheme.Check this one with a free account
Question 3 · CS-000735 marks

A science museum prints a 16-bit visitor number on every annual pass. The number is shown on the pass in hexadecimal; one pass shows 3F2A.

  1. (a)

    Convert 3F2A to binary.

    [2]2 marks

  2. (b)

    Convert 3F2A to decimal, showing your working.

    [2]2 marks

  3. (c)

    State the number of different visitor numbers that can be stored in 16 bits.

    [1]1 mark

Answer it, then compare with the markscheme.Check this one with a free account

Try a quick-fire question

One of 75 short multiple-choice questions on A1.2. Pick an answer to see why it's right or wrong.

The 8 × 8 image is stored with 1 bit per pixel, black = 1 and white = 0, each row from left to right. Which byte stores row 4?

8 by 8 grid of black and white pixels, rows numbered 1 to 8 from the top. Black pixels by row, left to right (1 = black): row 1 00000000, row 2 01111100, row 3 01000000, row 4 01111000, rows 5 to 7 01000000, row 8 00000000.

Flashcards for A1.2

The official deck for this part of the syllabus.

A1.2 Data representation and computer logic · front

Why do computers store data using only two states?

A1.2 Data representation and computer logic · front

Give three physical forms in which a computer stores the two states of a bit.

  • A1.2 Data representation and computer logic388 cards

On this page

  • Key terms
  • Notes
  • Questions
  • Quick fire
  • Flashcards

Published for A1.2

Exam-style questions
95
Notes pages
1
Quick-fire questions
75
Key terms
60
Counted on 20 October 2026Open it in Augment

More of IB Computer Science

  • A1.1 Computer hardware and operationBefore this in the Computer Science syllabus.
  • A1.3 Operating systems and control systemsAfter this in the Computer Science syllabus.
  • A1 Computer fundamentalsThe whole topic, with all 4 of its subtopics.
  • IB Computer ScienceThe Computer Science syllabus, assessment and what Augment has for it.

Carry on with A1.2 in Augment

With a free account, Augment remembers where you stopped and what you have practiced.

Start freeOpen A1.2 in Augment