represent each digit. Hexadecimal (or hex for short) was adopted to easily and compactly represent binary numbers. Hexadecimal is a base 16 numbering system with 16 symbols. 0 through 9 are used for the first ten symbols, representing decimal values 0 – 9, and the last six symbols are A, B, C, D, E, F representing decimal values 10 – 15. What comes after F? Just as in decimal we write 10 for ten, in hexadecimal we write 10 for sixteen since each position is associated with a power of 16.
The reason why hexadecimal is so well suited to representing binary numbers is that sixteen is a power of 2. This means binary digits can be grouped together and directly converted to a hexadecimal digit. Since sixteen is the fourth power of 2, four binary digits – a nibble – can be represented by a single hexadecimal digit. Conversion between binary and hexadecimal can then be done by sight and hexadecimal becomes a quick way to represent large binary quantities as well as an easy way to visualize bit patterns.
The table below shows the correspondence between binary, hexadecimal and decimal values:
| Binary | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Hexadecimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
| Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
To convert hex to binary, change each hex digit into a nibble (four bits), using the table above. Conversely, to convert binary to hex, divide the binary number into nibbles, starting on the right, and then change each group into the corresponding hex digit.
Throughout this manual, we've written hexadecimal numbers suffixed by a lower case letter h or prefixed by $ as the latter notation is the one supported by the NextBASIC Integer Expression evaluator.
The bits inside the computer are mostly grouped into sets of eight – these are called bytes. A single byte can represent any number from 0 to 255 decimal (11111111b or FFh). A single byte can also represent any character in the ZX Spectrum Next character set. Its value can be written with two hex digits.
Two bytes can be grouped together to make what is called a word. A word can be written using sixteen bits or four hex digits, and represents a number from 0 to 65535 decimal.
A byte is always eight bits, but words vary in length from computer to computer. In Sinclair computer tradition, 16-bit numbers are called words while 32-bit numbers are called long words.
Setting a bit means making a specific bit 1. Resetting a bit means making a specific bit 0. In digital logic, there is also a concept of "active low" and "active high". This means a signal becomes active when it is 0 or 1 respectively. The Z80n has an M̅R̅E̅Q̅ (or /MREQ) signal, for example. This is an "active low" signal; to distinguish them from "active high" signals, we usually write active low signals with a bar over their names (Or prefix them with a forward slash /). This means the Z80n indicates a memory cycle by making M̅R̅E̅Q̅ 0.
Our first introduction to binary and hex was in Chapter 7 which introduced Integer Expressions. Chapter 14 introduced the use of the BIN keyword. Chapter 16 showed us how useful binary was in defining colours with the PALETTE keyword while Chapters 23 and 24 with the introduction of binary bitmasks for the REG and OUT keywords and the memory address space showed the usefulness of hexadecimal.
In reality many keyword parameters are binary; As an example ATTR and RUN AT's decimal parameters are really decimal "translations" of the bits that are being set inside the computer's memory or the Next Registers that these keywords control.
ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.