[Beginner] — The base nobody counts in and everybody reads. Chosen for one mathematical reason, and used everywhere that reason applies.
Before you start
- You can convert between bases — number bases.
- You know what a bit and a byte are — binary.
What you will be able to do after this lesson:
- Read and write hex fluently, including the letter digits.
- Convert between hex and binary by sight, with no arithmetic.
- Explain why hex is used for memory dumps, colours and byte values.
- Recognise hex in the wild and know what it is describing.
1. Sixteen digits
Base 16 needs sixteen symbols. The first ten are the familiar digits; the rest borrow letters:
| Hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Dec | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
A–F are digits, not letters that happen to be there. A means the quantity ten, exactly as 7 means seven.
Hex is usually written with a marker so it is not mistaken for decimal: 0xFF, #FF, $FF or FFh depending on the convention.
2. The one reason hex exists
So one hex digit is exactly four binary digits — always, with no remainder and no carrying between them.
1010 0101 binary
^^^^ ^^^^
A 5 hex
You convert digit by digit, in either direction, with no arithmetic:
| Hex | Binary | Hex | Binary | |
|---|---|---|---|---|
| 0 | 0000 | 8 | 1000 | |
| 1 | 0001 | 9 | 1001 | |
| 2 | 0010 | A | 1010 | |
| 3 | 0011 | B | 1011 | |
| 4 | 0100 | C | 1100 | |
| 5 | 0101 | D | 1101 | |
| 6 | 0110 | E | 1110 | |
| 7 | 0111 | F | 1111 |
Learn those sixteen rows and hex becomes free. It is the only memorisation this topic asks for, and it pays off permanently.
NOTE
Compare with decimal, which has no such relationship. is not a power of 2, so converting binary to decimal requires actual arithmetic and mixes all the bits together. Changing one bit can change every decimal digit.
In hex, changing one bit changes exactly one hex digit. That locality is why memory dumps, colour codes and bit masks are all written in hex — you can see which bits moved.
3. A byte is two hex digits
A byte is 8 bits, and , so every byte is exactly two hex digits — from 00 to FF, covering all 256 values.
That correspondence is why hex is everywhere bytes are:
- Memory addresses —
0x7FFE4C2A1B30 - Colours —
#FF8800is redFF, green88, blue00 - MAC addresses —
00:1B:44:11:3A:B7 - Byte values in a hex dump — two characters per byte, so columns line up
- Bit masks —
0xFF00obviously means “the top byte”, which65280does not - Unicode code points —
U+1F600 - Hashes — a SHA-256 digest is 64 hex characters, that is 32 bytes
4. Reading hex in the wild
#FF8800 colour: full red, two-thirds green, no blue -> orange
0x0F the low four bits set
0xFF00 the high byte of a 16-bit value
0x80000000 the top bit of a 32-bit value -- the sign bit
0xDEADBEEF a deliberately recognisable marker value
That last one is a real convention. Programmers fill unused memory with distinctive hex patterns — 0xDEADBEEF, 0xCAFEBABE, 0xBAADF00D — because spotting one in a debugger immediately tells you that you are reading uninitialised or freed memory rather than real data.
5. Practice — independent task
- (a) Convert by sight, no arithmetic:
0x3F,0xC0,0xA7,0xFFFFto binary. - (b) Convert to hex by sight:
1101 0110,0000 1111,1111 0000 1010 0101. - (c) What colour is
#00FF00? What is#808080? What does#FFFFFFmean, and why? - (d) A 32-bit value is
0x80000000. Which single bit is set? What does that bit mean in a signed integer (two’s complement)? - (e) Write
mask(bits)returning the hex mask that selects the lownbits —mask(4)should give0xF,mask(8)gives0xFF,mask(12)gives0xFFF. Explain the pattern. - (f) Given
0xABand the mask0x0F, what does a bitwise AND produce? What does that operation extract, and why is it a common idiom?
Done when: you can convert both directions by sight without writing anything down, and can explain what masking with 0x0F extracts.
Hint for (e) and (f) — open only after an attempt
The mask for the low $n$ bits is $2^n - 1$, which in hex is a run ofFs (plus one partial digit when $n$ is not a multiple of 4). That is (1 << n) - 1 in most languages.0xAB & 0x0F keeps only the bits where the mask has 1s, giving 0x0B — the low nibble. Since one hex digit is a nibble, masking with 0x0F extracts the last hex digit. That is why bit manipulation is written in hex: the mask visually shows which digits survive.
Before moving on
- Recite the sixteen hex digits and their values.
- Convert hex to binary and back by sight.
- Explain why is the entire reason hex is used.
- Explain why a byte is exactly two hex digits.
- Read a colour code and a bit mask and say what each describes.
Recap: Hexadecimal uses sixteen digits, 0–9 then A–F. It exists because , so one hex digit is exactly four bits and conversion is digit-by-digit with no arithmetic — and a byte, being 8 bits, is exactly two hex digits. That locality means changing one bit changes exactly one hex digit, which is why memory dumps, colours, masks and addresses are all written in hex rather than decimal.
Next: Number systems — from how numbers are written to what kinds of number exist.
Related
- Number bases · Binary · Decimal
- computer-architecture/data representation — where these bytes get meaning
- How Computers Work — the course this unblocks
Software engineering
Infrastructure & security
Computer science
Hardware & engineering