Computer Number Systems
Binary, octal, decimal, and hexadecimal, and how to convert between them.
Overview
All digital computers are electronic devices that ultimately can do one thing: detect whether an electrical signal is on or off. This basic information, called a bit (binary digit), has two values:
- 1 (or true) when the signal is on
- 0 (or false) when the signal is off
Key Concepts
Number Systems
Binary (Base-2)
Uses digits: 0, 1
Example: 10110₂
Used for: Basic computer storage
Octal (Base-8)
Uses digits: 0-7
Example: 75021₈
Used for: Compact representation of binary
Decimal (Base-10)
Uses digits: 0-9
Example: 97425
Used for: Human calculations ("Standard" number system)
Hexadecimal/Hex (Base-16)
Uses digits: 0-9, A-F (10-15)
Example: 54A2DD₁₆
Used for: Compact binary representation, colors
Converting to Decimal
To convert a number from any base to decimal (base-10), multiply each digit by its place value and sum the results.
Converting from Decimal
To convert from decimal to another base:
- Integer divide the decimal number by the target base and set the remainder to the side.
- Repeat the process for the quotient until it becomes 0.
- The remainders will be the digits of the number in the new base, from bottom to top.
Quick Conversions Between Bases
There are shortcuts for converting between binary, octal, and hexadecimal without going through decimal!
- Binary ↔ Octal: Group binary digits by threes (from right).
- Binary ↔ Hexadecimal: Group binary digits by fours (from right).
- Octal ↔ Hexadecimal: Convert through binary as an intermediate step.
Pro Tip: Memorize these patterns:
- Octal: Each digit = 3 binary digits
- Hex: Each digit = 4 binary digits
- For Octal ↔ Hex: Use binary as a bridge!
Examples
Converting to Decimal
Binary to Decimal
11012 = (1×23) + (1×22) + (0×21) + (1×20) = 8 + 4 + 0 + 1 = 13
Octal to Decimal
1758 = (1×82) + (7×81) + (5×80) = 64 + 56 + 5 = 125
Hex to Decimal
A516 = (10×161) + (5×160) = 160 + 5 = 165
Converting from Decimal
Example: Convert 92 to Binary
92 ÷ 2 = 46 remainder 0 46 ÷ 2 = 23 remainder 0 23 ÷ 2 = 11 remainder 1 11 ÷ 2 = 5 remainder 1 5 ÷ 2 = 2 remainder 1 2 ÷ 2 = 1 remainder 0 1 ÷ 2 = 0 remainder 1
Reading from bottom up: 9210 = 10111002
Example: Convert 25847 to Hexadecimal
25847 ÷ 16 = 1615 remainder 7 1615 ÷ 16 = 100 remainder 15 (F) 100 ÷ 16 = 6 remainder 4 6 ÷ 16 = 0 remainder 6
Reading from bottom up: 2584710 = 64F716
Quick Conversions Between Bases
Binary ↔ Octal
Group binary digits by threes (from right)
Binary to Octal:
111 101 010 = 752₈
↓ ↓ ↓
7 5 2
Octal to Binary:
375₈ = 011 111 101₂
↓ ↓ ↓
3 7 5
Binary ↔ Hexadecimal
Group binary digits by fours (from right)
Binary to Hex:
1010 1111 = AF₁₆
↓ ↓
A F
Hex to Binary:
FD₁₆ = 1111 1101₂
↓ ↓
F D
Octal ↔ Hexadecimal
Convert through binary as an intermediate step:
Octal → Binary → Hex: 375₈ → 011111101₂ → FD₁₆ Hex → Binary → Octal: FD₁₆ → 11111101₂ → 375₈
Practice Problems
Problem 1 Junior
Convert 3676₈ to hexadecimal.
Solution
- Convert octal to binary: 3676₈ = 011 110 111 110₂
- Group by 4 bits: 0111 1011 1110₂
- Convert to hex: 7BE₁₆
Problem 2 Junior
How many 5's appear in total when writing all decimal numbers from 1 to 75 in octal?
Solution
- Convert each decimal number to octal:
1₁₀ = 1₈ 2₁₀ = 2₈ ... 45₁₀ = 55₈ 46₁₀ = 56₈ ... 75₁₀ = 113₈
- Count the 5's in each number:
- 45₁₀ = 55₈ (two 5's)
- 46₁₀ = 56₈ (one 5)
- 47₁₀ = 57₈ (one 5)
- 48₁₀ = 60₈ (no 5's)
- 51₁₀ = 63₈ (no 5's)
- 52₁₀ = 64₈ (no 5's)
- 53₁₀ = 65₈ (one 5)
- 54₁₀ = 66₈ (no 5's)
- 55₁₀ = 67₈ (no 5's)
- ...
- Total count: 15 occurrences of 5
Problem 3 Intermediate
Evaluate the expression and express the final answer in hex:
10₂ * 61₁₆ + 1001₂ * (1011₂ - A₁₆)
Solution
- Convert numbers to decimal:
- 10₂ = 2₁₀
- 61₁₆ = 97₁₀
- 1001₂ = 9₁₀
- 1011₂ = 11₁₀
- A₁₆ = 10₁₀
- Solve the parentheses: (11₁₀ - 10₁₀) = 1₁₀
- Multiply: 2₁₀ * 97₁₀ = 194₁₀
- Multiply: 9₁₀ * 1₁₀ = 9₁₀
- Add: 194₁₀ + 9₁₀ = 203₁₀
- Convert to hex: 203₁₀ = CB₁₆
Problem 4 Intermediate
Convert 2023₁₀ to octal. Write the octal digits in ascending order.
Then convert this reordered octal number to hexadecimal.
Solution
- Convert 2023₁₀ to octal:
2023 ÷ 8 = 252 remainder 7 252 ÷ 8 = 31 remainder 4 31 ÷ 8 = 3 remainder 7 3 ÷ 8 = 0 remainder 3
Reading from bottom up: 2023₁₀ = 3747₈ - Reorder octal digits in ascending order: 3477₈
- Convert 3477₈ to binary:
3 → 011 4 → 100 7 → 111 7 → 111
3477₈ = 011100111111₂ - Group binary digits by fours and convert to hex:
0111 0011 1111 7 3 F
Final answer: 73F₁₆
Problem 5 Senior
Evaluate and express in base 16 if both numbers are hexadecimal:
1F * 4D
Solution
- Convert hex numbers to decimal:
- 1F₁₆ = (1×16¹) + (15×16⁰) = 16 + 15 = 31₁₀
- 4D₁₆ = (4×16¹) + (13×16⁰) = 64 + 13 = 77₁₀
- Multiply in decimal:
31₁₀ × 77₁₀ = 2,387₁₀
- Convert result to hexadecimal:
2387 ÷ 16 = 149 remainder 3 149 ÷ 16 = 9 remainder 5 9 ÷ 16 = 0 remainder 9
Reading from bottom up: 2387₁₀ = 953₁₆