A levels Computer Science · Data representation
Number systems and binary arithmetic
1Number bases
| Base | Name | Digits | Example |
|---|---|---|---|
| 2 | binary | 0, 1 | 0110 1011 |
| 10 | denary | 0–9 | 107 |
| 16 | hexadecimal | 0–9, A–F (A = 10 ... F = 15) | 6B |
Converting 0110 1011
- Denary: 64 + 32 + 8 + 2 + 1 = 107.
- Hex: split into nibbles (groups of 4 bits): 0110 = 6, 1011 = 11 = B, so 6B.
- Check: 6 × 16 + 11 = 107.
Hexadecimal is used because it is shorter and easier for people to read than binary, and converts to binary easily (one hex digit = 4 bits). It is used for colour codes (#FF8800), memory addresses and MAC addresses.
Check yourself
What is hex 3E in denary?
Show the answer
62
3 × 16 + 14.
2Units of storage
| Prefix | Symbol | Value | Prefix | Symbol | Value |
|---|---|---|---|---|---|
| kilo | kB | 10³ | kibi | KiB | 2¹⁰ = 1024 |
| mega | MB | 10⁶ | mebi | MiB | 2²⁰ |
| giga | GB | 10⁹ | gibi | GiB | 2³⁰ |
| tera | TB | 10¹² | tebi | TiB | 2⁴⁰ |
A bit is one binary digit. A byte is 8 bits. With n bits, you can represent 2ⁿ different values.
Check yourself
How many different values can 10 bits represent?
Show the answer
1024
2¹⁰.
3Binary addition and overflow
Rules: 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 0 carry 1, 1 + 1 + 1 = 1 carry 1.
0110 1100 + 0011 1010
- Add column by column from the right, carrying as you go.
- Result: 1010 0110 = 166.
- Check: 108 + 58 = 166.
Overflow happens when a result is too big for the number of bits. In 8-bit unsigned binary the largest value is 255; in 8-bit two's complement it is 127, so 108 + 58 = 166 overflows if the numbers are signed (the result looks negative).
Check yourself
What is 1011 + 0110 in binary (unsigned)?
Show the answer
10001
11 + 6 = 17.
4Two's complement
Two's complement represents negative integers. The most significant bit has a negative place value.
−45 in 8-bit two's complement is 1101 0011. Only the most significant bit has a negative place value.
To find the two's complement of a number (to negate it): invert all the bits, then add 1.
−45 in 8 bits
- 45 = 0010 1101.
- Invert: 1101 0010.
- Add 1: 1101 0011.
- Check: −128 + 64 + 16 + 2 + 1 = −45.
The range of n-bit two's complement is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1: for 8 bits, −128 to 127.
Check yourself
What denary value is the 8-bit two's complement number 1111 1110?
Show the answer
−2
−128 + 126.
5Subtraction and shifts
To subtract, add the two's complement of the second number and ignore any carry out of the leftmost bit.
50 − 20 in 8 bits
- 50 = 0011 0010. 20 = 0001 0100, so −20 = 1110 1100.
- 0011 0010 + 1110 1100 = 1 0001 1110.
- Ignore the carry out: 0001 1110 = 30.
Shifts move every bit:
- A logical left shift by 1 multiplies an unsigned number by 2 (0000 0101 = 5 → 0000 1010 = 10).
- A logical right shift by 1 divides by 2, losing the rightmost bit.
- An arithmetic right shift copies the sign bit in, so it works for negative two's complement numbers.
Binary multiplication works like long multiplication: shift and add.
Check yourself
What is the result of a logical left shift by 2 on 0000 0011?
Show the answer
0000 1100 (12)
× 4.
6Fixed-point binary fractions
In fixed point, the binary point is in a fixed position. Bits after the point have values ½, ¼, ⅛, ...
0110.1100
- Before the point: 0110 = 6.
- After the point: ½ + ¼ = 0.75.
- Value = 6.75.
Some fractions cannot be stored exactly: 0.1 in binary is 0.000110011... recurring. With 4 bits after the point, the closest value is 0.0010 = 0.125, or 0.0001 = 0.0625.
Fixed point is simple and fast, but its range is small for a given number of bits. Floating point solves this.
Check yourself
What is the fixed-point number 0011.0110 in denary?
Show the answer
3.375
3 + 0.25 + 0.125.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera