Hexadecimal numbers: Base sixteen
Hexadecimal is a short way to write binary values. Z80 assembly uses it for bytes and addresses.
Number basics
Sixteen digits
Hexadecimal uses 0–9 and A–F. The letters mean 10–15. Each place is a power of 16. In Z80 code, an h after a number marks it as hexadecimal.
2Ah = 2 × 16 + 10 = 42
Choose a byte
Two hex digits make one byte
A nibble is four bits. One hex digit represents one nibble. The first digit is the high nibble. The second is the low nibble.
Place values in a 16-bit number
| Hex place | 16³ | 16² | 16¹ | 16⁰ |
|---|---|---|---|---|
| Digit in 1234h | 1 | 2 | 3 | 4 |
| Place value | 4096 | 256 | 16 | 1 |
| Contribution | 4096 | 512 | 48 | 4 |
Add the contributions: 4096 + 512 + 48 + 4 = 4660. So 1234h is 4660 in decimal.
Useful byte examples
| Hex | Calculation | Decimal | Binary |
|---|---|---|---|
| 10h | 1 × 16 + 0 | 16 | 0001 0000 |
| 2Ah | 2 × 16 + 10 | 42 | 0010 1010 |
| FFh | 15 × 16 + 15 | 255 | 1111 1111 |
Numbers 1 to 16 in binary and hexadecimal
The binary column uses a full byte. The hexadecimal column uses two digits for the same 8-bit value.
| Decimal | 8-bit binary | Hexadecimal |
|---|---|---|
| 1 | 0000 0001 | 01h |
| 2 | 0000 0010 | 02h |
| 3 | 0000 0011 | 03h |
| 4 | 0000 0100 | 04h |
| 5 | 0000 0101 | 05h |
| 6 | 0000 0110 | 06h |
| 7 | 0000 0111 | 07h |
| 8 | 0000 1000 | 08h |
| 9 | 0000 1001 | 09h |
| 10 | 0000 1010 | 0Ah |
| 11 | 0000 1011 | 0Bh |
| 12 | 0000 1100 | 0Ch |
| 13 | 0000 1101 | 0Dh |
| 14 | 0000 1110 | 0Eh |
| 15 | 0000 1111 | 0Fh |
| 16 | 0001 0000 | 10h |
The byte examples on this page are unsigned. See Negative numbers and two’s complement to learn how fixed-width values can also represent signed numbers.