Internal data representation and number systems
Inside a computer everything is stored as binary digits (0 and 1); number systems and character codes turn these bits into numbers and text.
Why binary?
A computer is built from millions of tiny electronic switches. Each switch is either off (0) or on (1), so two digits are enough. Eight bits make a byte. One byte can hold 2⁸ = 256 different patterns, enough for one letter, digit or symbol.
The four number systems
| System | Base | Digits used | Example |
|---|---|---|---|
| Binary | 2 | 0, 1 | 1011₂ |
| Octal | 8 | 0 to 7 | 17₈ |
| Decimal | 10 | 0 to 9 | 245₁₀ |
| Hexadecimal | 16 | 0 to 9, then A=10, B=11, C=12, D=13, E=14, F=15 | 3F₁₆ |
Decimal to binary: divide by 2
Convert 45₁₀ to binary.
- 45 ÷ 2 = 22, remainder 1
- 22 ÷ 2 = 11, remainder 0
- 11 ÷ 2 = 5, remainder 1
- 5 ÷ 2 = 2, remainder 1
- 2 ÷ 2 = 1, remainder 0
- 1 ÷ 2 = 0, remainder 1
- Read the remainders from the bottom up: 101101
- Check: 32 + 8 + 4 + 1 = 45
45₁₀ = 101101₂
Binary to decimal: use place values
Convert 110101₂ to decimal.
- Place values from the right: 1, 2, 4, 8, 16, 32
- Write each bit under its place value: 32→1, 16→1, 8→0, 4→1, 2→0, 1→1
- 1×32 + 1×16 + 0×8 + 1×4 + 0×2 + 1×1
- = 32 + 16 + 0 + 4 + 0 + 1 = 53
110101₂ = 53₁₀
Decimal to octal: divide by 8
Convert 100₁₀ to octal.
- 100 ÷ 8 = 12, remainder 4
- 12 ÷ 8 = 1, remainder 4
- 1 ÷ 8 = 0, remainder 1
- Read the remainders from the bottom up: 144
- Check: 1×64 + 4×8 + 4×1 = 64 + 32 + 4 = 100
100₁₀ = 144₈
Decimal to hexadecimal: divide by 16
Convert 250₁₀ to hexadecimal.
- 250 ÷ 16 = 15, remainder 10
- 15 ÷ 16 = 0, remainder 15
- Read from the bottom up: 15, 10
- Change to hex digits: 15 = F, 10 = A
- Check: F×16 + A×1 = 15×16 + 10 = 240 + 10 = 250
250₁₀ = FA₁₆
Binary to octal and hexadecimal: group the bits
Convert 10111110₂ to octal and to hexadecimal.
- Octal: group in 3s from the right, adding a 0 in front: 010 111 110
- 010 = 2, 111 = 7, 110 = 6, so the answer is 276₈
- Hexadecimal: group in 4s from the right: 1011 1110
- 1011 = 11 = B, 1110 = 14 = E, so the answer is BE₁₆
- Check in decimal: 2×64 + 7×8 + 6 = 128 + 56 + 6 = 190
- and B×16 + E = 11×16 + 14 = 176 + 14 = 190
10111110₂ = 276₈ = BE₁₆ = 190₁₀
Character codes
| Code | Size | What it covers |
|---|---|---|
| ASCII | 7 bits, 128 characters (extended ASCII uses 8 bits) | English letters, digits and symbols. 'A' = 65 = 01000001 |
| EBCDIC | 8 bits | Used mainly on IBM mainframe computers |
| Unicode | 1 to 4 bytes per character (UTF-8 form) | Almost every writing system, including Sinhala and Tamil, plus symbols and emoji |
Sri Lankan government and business websites can show Sinhala, Tamil and English on the same page because all three scripts are coded in Unicode. Web designers also write colours in hexadecimal, for example #FFFFFF for white.
Structured question: “Convert 156₁₀ into binary, octal and hexadecimal. Show your working.” 156₁₀ = 10011100₂ = 234₈ = 9C₁₆. Show the repeated division by 2, group the bits in 3s and 4s, and check by place values.
Short answer: “What is a byte?” A group of 8 bits. It can hold 256 different patterns, enough to store one character.
Short answer: “Why do computers use the binary system?” Computer circuits are switches with two states, off and on, which match the two binary digits 0 and 1.
To go from decimal: divide and read the remainders upward. To come back to decimal: multiply by place values and add. Shortcut: binary to octal in 3s, binary to hex in 4s.