OCR H446, section 1.4.1 (e)
Hexadecimal (hex) is base 16. It uses 16 different digits, and each place is worth 16 times the one to its right.
The digits
There are only 10 ordinary digits, so hex uses the letters A to F for the extra six:
| Denary | Hex | Binary |
|---|---|---|
| 0 | 0 | 0000 |
| 1 | 1 | 0001 |
| 2 | 2 | 0010 |
| 3 | 3 | 0011 |
| 4 | 4 | 0100 |
| 5 | 5 | 0101 |
| 6 | 6 | 0110 |
| 7 | 7 | 0111 |
| 8 | 8 | 1000 |
| 9 | 9 | 1001 |
| 10 | A | 1010 |
| 11 | B | 1011 |
| 12 | C | 1100 |
| 13 | D | 1101 |
| 14 | E | 1110 |
| 15 | F | 1111 |
Notice that each hex digit matches exactly 4 bits (one nibble), because .
Place values
| 4096 | 256 | 16 | 1 |
Example:
| 256 | 16 | 1 |
|---|---|---|
| 3 | A (10) | 7 |
The base is written as a subscript, or the number is written with a prefix such as 0x (as in 0x3A7) or # (for colours).
Two hex digits make a byte
A byte is 8 bits, which is two nibbles, so two hex digits represent a byte:
So the range of one byte in hex is 00 to FF.
Why is hex used?
Hex is used as a shorthand for binary.
- Binary numbers are long and hard for people to read, remember and copy without mistakes.
1011011110100101is hard to work with, butB7A5is short. - Converting between hex and binary is simple, because each hex digit is exactly 4 bits, so there is no calculation (see 1.4.1 (f)).
- It is more compact: it takes a quarter of the characters of the binary.
- It is less error-prone than long strings of 0s and 1s.
Hex is not faster for the computer, which still works entirely in binary. It is purely a convenience for humans.
Where hex is used
| Use | Example |
|---|---|
| Memory addresses | 0x7FFE4A20 |
| Colour codes in web pages (red, green, blue, one byte each) | #FF8800 is orange: red FF (255), green 88 (136), blue 00 (0) |
| MAC addresses | 3C:22:FB:4A:91:0E |
| Error codes and machine code listings | 0xC0000005 |
| Character codes | 0x41 is A |
| Debugging and viewing raw data | A hex dump of a file |
Worked example
Task: (a) what denary value is ? (b) A web colour is #1A2B3C. What are the denary values of its red, green and blue parts?
- (a) .
- (b) The colour has three bytes:
- red
1A: - green
2B: - blue
3C:
- red
Exam tip: give the reason for hex as a shorter, easier to read and less error-prone way of writing binary, and say that each hex digit represents 4 bits. Don't say it is faster for the computer.
Check yourself
What is the denary value of ?
How many bits does one hexadecimal digit represent?
Four bits.
What is in denary?
.
Give one reason why programmers use hexadecimal instead of binary
It is a shorter, more readable way of writing binary values, and easy to convert, because each hex digit is exactly 4 bits.