Edexcel International Advanced Level Computer Science, point 2.1.1
Definition
A number base (or radix) is the number of different digits a counting system uses, and the value each place is worth.
| Base | Name | Digits | Place values (right to left) |
|---|---|---|---|
| 2 | Binary | 0, 1 | 1, 2, 4, 8, 16, 32, 64, 128, ... (powers of 2) |
| 10 | Denary (decimal) | 0 to 9 | 1, 10, 100, 1000, ... (powers of 10) |
| 16 | Hexadecimal | 0 to 9, A to F | 1, 16, 256, 4096, ... (powers of 16) |
In hexadecimal, A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.
Function and why needed
- Binary is how a computer stores and processes everything. A bit has two states (on or off, high or low voltage), which is reliable and cheap to build in hardware.
- Denary is what people use.
- Hexadecimal is a compact, human-friendly way to write binary. One hex digit stands for exactly four bits (a nibble), so a byte is two hex digits. It is used for memory addresses, colour codes (such as
#FF8800), MAC addresses and error codes.
| Binary | Hex | Denary | Binary | Hex | Denary | |
|---|---|---|---|---|---|---|
| 0000 | 0 | 0 | 1000 | 8 | 8 | |
| 0001 | 1 | 1 | 1001 | 9 | 9 | |
| 0010 | 2 | 2 | 1010 | A | 10 | |
| 0011 | 3 | 3 | 1011 | B | 11 | |
| 0100 | 4 | 4 | 1100 | C | 12 | |
| 0101 | 5 | 5 | 1101 | D | 13 | |
| 0110 | 6 | 6 | 1110 | E | 14 | |
| 0111 | 7 | 7 | 1111 | F | 15 |
Converting unsigned integers
Unsigned means there is no sign: the number is zero or positive.
Binary to denary
Add the place values where there is a 1.
11010110
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 0 | 1 | 0 | 1 | 1 | 0 |
Denary to binary
Method 1: subtract place values. Take the largest place value that fits, write a 1, subtract it, and carry on.
Method 2: repeated division by 2. Divide by 2, write down the remainder, repeat until you reach 0, then read the remainders from the bottom up.
200 to binary:
| Division | Quotient | Remainder |
|---|---|---|
| 200 ÷ 2 | 100 | 0 |
| 100 ÷ 2 | 50 | 0 |
| 50 ÷ 2 | 25 | 0 |
| 25 ÷ 2 | 12 | 1 |
| 12 ÷ 2 | 6 | 0 |
| 6 ÷ 2 | 3 | 0 |
| 3 ÷ 2 | 1 | 1 |
| 1 ÷ 2 | 0 | 1 |
Reading the remainders from the bottom: 11001000.
The same method, written as steps an algorithm could follow:
- Start with an empty answer.
- Find the remainder when the number is divided by 2 (0 or 1), and write it in front of the answer so far.
- Replace the number with the whole-number result of dividing it by 2.
- Repeat steps 2 and 3 until the number is 0.
Binary to hexadecimal
Split into groups of four bits from the right, and convert each group.
11010110 becomes 1101 0110, which is D and 6, so D6.
Hexadecimal to binary
Replace each hex digit with its four bits. B7 is 1011 0111, so 10110111.
Hexadecimal to denary
Multiply each digit by its place value (1, 16, 256, ...).
B7
Denary to hexadecimal
Divide by 16 repeatedly, or convert to binary and then group. remainder , so D6.
Going through binary
Converting through binary works in both directions: split the bits into nibbles, one hex digit for each nibble.
Benefits and drawbacks of hexadecimal
- Benefit: much shorter and less error-prone to read and write than long binary strings, with an easy conversion to and from binary.
- Drawback: the computer still works in binary, so it is only a notation for people, and it's harder to read as a quantity than denary.
Worked example
Task: a sensor sends the byte 10101110. (a) Give it in hexadecimal and in denary. (b) Convert the denary number 90 to 8-bit binary by repeated division.
(a) Hexadecimal: split into nibbles, 1010 1110, which are 10 and 14, so AE.
Denary: . Check from the hex: . ✓
(b)
| Division | Quotient | Remainder |
|---|---|---|
| 90 ÷ 2 | 45 | 0 |
| 45 ÷ 2 | 22 | 1 |
| 22 ÷ 2 | 11 | 0 |
| 11 ÷ 2 | 5 | 1 |
| 5 ÷ 2 | 2 | 1 |
| 2 ÷ 2 | 1 | 0 |
| 1 ÷ 2 | 0 | 1 |
Reading from the bottom gives 1011010, so in 8 bits 01011010. Check: . ✓
Exam tip: show your working (place values, or the table of remainders) so that you can earn method marks even if you slip. Check hex to binary by grouping four bits. Pad binary answers to the full width asked for (for example 8 bits). For hex, remember A to F are 10 to 15, and write both digits even if you're unsure of one, because each digit can earn a mark. Always check your answer by converting it back.
Check yourself
Convert 10110010 from binary to denary
128 + 32 + 16 + 2 = 178.
Convert 75 from denary to 8-bit binary
01001011 (64 + 8 + 2 + 1).
Convert 10111100 to hexadecimal
BC (1011 is B, 1100 is C).
Convert hexadecimal 4E to denary
4 × 16 + 14 = 78.
Why is hexadecimal used instead of binary for memory addresses?
It is much shorter and easier for people to read and write, and each hex digit converts directly to four bits.