<!-- PDF page 296 -->

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:

<table>
<tbody>
<tr><th>Binary</th><td>0000</td><td>0001</td><td>0010</td><td>0011</td><td>0100</td><td>0101</td><td>0110</td><td>0111</td><td>1000</td><td>1001</td><td>1010</td><td>1011</td><td>1100</td><td>1101</td><td>1110</td><td>1111</td></tr>
<tr><th>Hexadecimal</th><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td></tr>
<tr><th>Decimal</th><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td></tr>
</tbody>
</table>

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.

### Bits, Bytes and Words

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**.

### Using Binary and Hex in NextBASIC

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.

