Number Systems
IB Syllabus: A1.2.1 — Describe the principal methods of representing data in a computer system: binary, hexadecimal, decimal integers; and conversions between them.
Table of Contents
- Key Concepts
- Worked Examples
- Quick Code Check
- Trace Exercise
- Spot the Error
- Fill in the Blanks
- Predict the Output
- Practice Exercises
- Connections
Key Concepts
Binary Number System (Base 2)
The binary number system uses only two digits: 0 and 1. Each digit is called a bit (binary digit).
Computers use binary because their electronic circuits have two stable states — on (high voltage, represented as 1) and off (low voltage, represented as 0). Every piece of data a computer stores, processes, or transmits is ultimately a sequence of 0s and 1s.
Place Values
In the decimal system you already know, each position represents a power of 10 (ones, tens, hundreds…). In binary, each position represents a power of 2:
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Power of 2 | 2^7 | 2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 |
An 8-bit binary number can represent values from 0 (00000000) to 255 (11111111).
Counting in Binary
| Decimal | Binary | Explanation |
|---|---|---|
| 0 | 0000 | All bits off |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 2 + 1 |
| 4 | 0100 | 4 |
| 5 | 0101 | 4 + 1 |
| 6 | 0110 | 4 + 2 |
| 7 | 0111 | 4 + 2 + 1 |
| 8 | 1000 | 8 |
Notice the pattern: the rightmost bit toggles every number, the next bit toggles every 2 numbers, the next every 4, and so on — exactly like the ones, tens, and hundreds columns in decimal.
Hexadecimal Number System (Base 16)
The hexadecimal (hex) system uses sixteen digits: 0-9 for values zero to nine, then A-F for values ten to fifteen.
| Hex digit | A | B | C | D | E | F |
|---|---|---|---|---|---|---|
| Decimal value | 10 | 11 | 12 | 13 | 14 | 15 |
Why Does Hexadecimal Exist?
Binary numbers are long and difficult for humans to read. Hexadecimal provides a compact representation of binary — exactly 4 binary digits map to 1 hex digit. This makes hex much shorter and less error-prone to read, while still directly reflecting the underlying binary.
11111111in binary =FFin hex = 255 in decimal- Two hex digits represent one byte (8 bits)
Common Uses of Hexadecimal
- Colour codes — web colours use hex:
#FF0000is red (255 red, 0 green, 0 blue),#00FF00is green,#0000FFis blue - Memory addresses — RAM locations are displayed in hex (e.g.,
0x7FFF5A2C) - MAC addresses — network hardware addresses use hex (e.g.,
00:1A:2B:3C:4D:5E) - Error codes — system and hardware error codes are often shown in hex
- Character encoding — Unicode code points are written in hex (e.g., U+0041 = ‘A’)
Decimal to Binary Conversion
To convert a decimal number to binary, use the repeated division by 2 method:
- Divide the number by 2
- Record the remainder (0 or 1)
- Replace the number with the quotient (integer result)
- Repeat until the quotient is 0
- Read the remainders bottom to top — this is the binary result
Example: Convert 157 to binary.
| Step | Division | Quotient | Remainder |
|---|---|---|---|
| 1 | 157 / 2 | 78 | 1 |
| 2 | 78 / 2 | 39 | 0 |
| 3 | 39 / 2 | 19 | 1 |
| 4 | 19 / 2 | 9 | 1 |
| 5 | 9 / 2 | 4 | 1 |
| 6 | 4 / 2 | 2 | 0 |
| 7 | 2 / 2 | 1 | 0 |
| 8 | 1 / 2 | 0 | 1 |
Reading the remainders bottom to top: 10011101
Verification: 128 + 16 + 8 + 4 + 1 = 157
Binary to Decimal Conversion
To convert a binary number to decimal, multiply each bit by its place value and sum the results.
Example: Convert 10110011 to decimal.
| Bit position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Bit | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
| Contribution | 128 | 0 | 32 | 16 | 0 | 0 | 2 | 1 |
Sum: 128 + 32 + 16 + 2 + 1 = 179
Decimal to Hexadecimal Conversion
To convert a decimal number to hexadecimal, use the repeated division by 16 method:
- Divide the number by 16
- Record the remainder — if 10-15, convert to A-F
- Replace the number with the quotient
- Repeat until the quotient is 0
- Read the remainders bottom to top
Example: Convert 255 to hexadecimal.
| Step | Division | Quotient | Remainder | Hex digit |
|---|---|---|---|---|
| 1 | 255 / 16 | 15 | 15 | F |
| 2 | 15 / 16 | 0 | 15 | F |
Reading bottom to top: FF
Verification: F(15) x 16 + F(15) x 1 = 240 + 15 = 255
Hexadecimal to Decimal Conversion
To convert a hexadecimal number to decimal, multiply each hex digit by its place value (powers of 16) and sum the results.
Example: Convert 2A to decimal.
| Position | 1 | 0 |
|---|---|---|
| Place value | 16 | 1 |
| Hex digit | 2 | A (= 10) |
| Contribution | 2 x 16 = 32 | 10 x 1 = 10 |
Sum: 32 + 10 = 42
Binary to Hexadecimal Shortcut
Because 16 = 2^4, exactly 4 binary digits map to 1 hexadecimal digit. This gives us a fast shortcut:
Binary to Hex:
- Split the binary number into groups of 4 bits (from the right)
- Convert each 4-bit group to its hex equivalent
Hex to Binary:
- Convert each hex digit to its 4-bit binary equivalent
- Concatenate the results
Reference Table:
| Binary | Hex | Binary | Hex |
|---|---|---|---|
| 0000 | 0 | 1000 | 8 |
| 0001 | 1 | 1001 | 9 |
| 0010 | 2 | 1010 | A |
| 0011 | 3 | 1011 | B |
| 0100 | 4 | 1100 | C |
| 0101 | 5 | 1101 | D |
| 0110 | 6 | 1110 | E |
| 0111 | 7 | 1111 | F |
Example: Convert 10101111 to hexadecimal.
Split into 4-bit groups: 1010 1111
1010= A1111= F
Result: AF
Example: Convert C7 to binary.
- C =
1100 - 7 =
0111
Result: 11000111
Enrichment: Storage Units
This goes beyond conversion skills but provides useful context for later data representation topics.
Data is measured in units based on the byte (8 bits):
| Unit | Symbol | Size |
|---|---|---|
| Bit | b | 1 binary digit (0 or 1) |
| Byte | B | 8 bits |
| Kilobyte | KB | 1,024 bytes (2^10) |
| Megabyte | MB | 1,024 KB (2^20 bytes) |
| Gigabyte | GB | 1,024 MB (2^30 bytes) |
| Terabyte | TB | 1,024 GB (2^40 bytes) |
The IB typically uses binary prefixes (1 KB = 1,024 bytes) rather than decimal prefixes (1 kB = 1,000 bytes). In everyday use, storage manufacturers often use decimal prefixes, which is why a “500 GB” hard drive may show as approximately 465 GB on your computer.
With n bits, you can represent 2^n different values. For example:
- 8 bits = 256 different values (0 to 255)
- 16 bits = 65,536 different values
- 32 bits = approximately 4.3 billion different values
Enrichment: Two’s Complement
This goes beyond basic number system conversions but is important for understanding how computers handle negative numbers.
All the binary numbers discussed so far are unsigned — they represent positive values only. But computers also need to represent negative numbers. The standard method is two’s complement.
How Two’s Complement Works
In two’s complement, the most significant bit (leftmost bit) acts as the sign bit:
- 0 in the sign bit = positive number
- 1 in the sign bit = negative number
Converting a Positive Number to Its Negative (Two’s Complement)
- Write the positive number in binary
- Flip all the bits (0 becomes 1, 1 becomes 0) — this is called the one’s complement
- Add 1 to the result
Example: Represent -42 in 8-bit two’s complement.
| Step | Operation | Result |
|---|---|---|
| 1. Write 42 in binary | 42 = 32 + 8 + 2 | 00101010 |
| 2. Flip all bits | Invert each bit | 11010101 |
| 3. Add 1 | 11010101 + 1 | 11010110 |
So -42 in 8-bit two’s complement is 11010110.
Range of Two’s Complement
With n bits in two’s complement:
- Minimum value: -2^(n-1)
- Maximum value: 2^(n-1) - 1
| Bits | Minimum | Maximum |
|---|---|---|
| 8 | -128 | +127 |
| 16 | -32,768 | +32,767 |
| 32 | -2,147,483,648 | +2,147,483,647 |
The range is not symmetrical because zero takes up one of the positive slots. In 8-bit two’s complement, there are 128 negative values (-128 to -1), zero, and 127 positive values (1 to 127) — a total of 256 values, which is exactly 2^8.
Worked Examples
Example 1: Decimal to Binary (Division Method)
Problem: Convert decimal 157 to binary.
Step 1: Set up the repeated division by 2 table.
| Division | Quotient | Remainder |
|---|---|---|
| 157 / 2 | 78 | 1 |
| 78 / 2 | 39 | 0 |
| 39 / 2 | 19 | 1 |
| 19 / 2 | 9 | 1 |
| 9 / 2 | 4 | 1 |
| 4 / 2 | 2 | 0 |
| 2 / 2 | 1 | 0 |
| 1 / 2 | 0 | 1 |
Step 2: Read the remainders from bottom to top: 10011101
Step 3: Verify by converting back to decimal: 128 + 16 + 8 + 4 + 1 = 157
Example 2: Binary to Decimal (Place Value Method)
Problem: Convert binary 10110011 to decimal.
Step 1: Write out the place values for each bit position.
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
Step 2: Identify which place values have a 1: 128, 32, 16, 2, 1
Step 3: Sum: 128 + 32 + 16 + 2 + 1 = 179
Example 3: Two’s Complement Decoding (Enrichment)
Problem: The 8-bit two’s complement binary number 11010110 represents a negative number. Find its decimal value.
Step 1: Check the sign bit. The leftmost bit is 1, so this is a negative number.
Step 2: To find the magnitude, apply two’s complement in reverse — flip all bits and add 1.
| Operation | Result |
|---|---|
| Original | 11010110 |
| Flip all bits | 00101001 |
| Add 1 | 00101010 |
Step 3: Convert 00101010 to decimal: 32 + 8 + 2 = 42
Step 4: Apply the negative sign: the answer is -42.
Quick Code Check
Q1. What is the decimal value of binary 1101?
Q2. What is the binary representation of decimal 25?
Q3. Why is hexadecimal commonly used in computing?
Q4. What is the hexadecimal equivalent of binary 1111 0000?
Q5. In 8-bit two's complement, what is the range of values that can be represented?
Trace Exercise
Convert the decimal number 203 to binary using the repeated division by 2 method. At each step, divide the current number by 2, record the quotient and remainder, then continue with the quotient.
Read the remainders from bottom to top to form the final binary number.
| Step | Number / 2 | Quotient | Remainder |
|---|---|---|---|
| 1 | 203 / 2 | ||
| 2 | 101 / 2 | ||
| 3 | 50 / 2 | ||
| 4 | 25 / 2 | ||
| 5 | 12 / 2 | ||
| 6 | 6 / 2 | ||
| 7 | 3 / 2 | ||
| 8 | 1 / 2 |
Spot the Error
A student converts decimal 156 to hexadecimal. They wrote the following steps. Click the line with the error, then pick the fix.
Pick the correct fix for this line:
Fill in the Blanks
Complete the following statements about number systems:
Number System Basics
====================
Binary is a base number system.
Hexadecimal uses digits 0-9 and letters to .
Conversions
===========
To convert decimal to binary, repeatedly divide by .
Each group of binary digits maps to one hexadecimal digit.
Storage
=======
1 byte = bits. Predict the Output
Convert the decimal number 75 to hexadecimal.
75 / 16 = 4 remainder 11
11 in hexadecimal = BWhat is the hexadecimal result?
Practice Exercises
Core
- Decimal to Binary — Convert each of the following decimal numbers to binary using the repeated division method:
- 42
- 100
- 200
- 13
- 255
- Binary to Decimal — Convert each of the following binary numbers to decimal using the place value method:
1010111001000000111111111
- Hex and Binary — Perform the following conversions using the 4-bit grouping shortcut:
- Convert hexadecimal
A3to binary - Convert binary
1100 0101to hexadecimal
- Convert hexadecimal
Extension
-
Two’s Complement — Represent -25 in 8-bit two’s complement. Show all three steps (write positive, flip bits, add 1) and verify your answer.
-
Why Hexadecimal? — Explain why hexadecimal is preferred over binary for displaying memory addresses. Consider readability, error-proneness, and the relationship between hex digits and bytes.
-
Address Space — A computer uses 16-bit memory addresses. How many unique memory addresses can it store? Express your answer in both decimal and hexadecimal.
Challenge
- Storage Calculations — Calculate how many bytes are needed to store each of the following. Compare the three storage requirements and express the largest in a more appropriate unit (KB, MB, or GB):
- 1,000 ASCII characters (1 byte per character)
- A 1920 x 1080 image at 24-bit colour depth (3 bytes per pixel)
- 1 minute of CD-quality audio (44,100 Hz sampling rate, 16-bit depth, stereo = 2 channels)
Connections
- Prerequisites: None (foundational topic)
- Related: Logic Gates — gates process binary signals
- Related: Primary Memory — memory stores binary data
- Forward: Representing Data — applies binary to encode all data types