# Hexadecimal

*Binary in a readable shorthand*

Hexadecimal is the base-16 number system, using the digits 0 to 9 then A to F. One hex digit encodes exactly four bits, making it a compact, readable shorthand for the bytes and memory addresses a computer works with.

Group: Number & logic
URL: https://digiwleea.wleeaf.dev/learn/hexadecimal/

From [binary numbers](https://digiwleea.wleeaf.dev/learn/binary/) you can read any string of bits as a value. The trouble is that the strings get long fast: a byte is eight bits, an address can be sixteen, and `0110 1011 1010 0101` is painful to read, copy, or say aloud. **Hexadecimal** (base 16, "hex") is the shorthand that fixes this. It is not a new way for the computer to count, just a tidier way for **us** to write the same bits. Convert between binary, decimal, and hex with the [binary converter](https://digiwleea.wleeaf.dev/tools/binary-converter/).

## Sixteen symbols, one per nibble

Base 16 needs sixteen digit symbols. We borrow `0`-`9` for the first ten and then the letters `A`-`F` for ten through fifteen. The magic is that `16 = 2^4`, so **one hex digit corresponds to exactly four bits** (one [nibble](https://digiwleea.wleeaf.dev/learn/binary/)). That makes converting a pure lookup, no arithmetic:

| binary | decimal | hex |
| --- | --- | --- |
| 0000 | 0 | 0 |
| 0001 | 1 | 1 |
| 0010 | 2 | 2 |
| 0011 | 3 | 3 |
| 0100 | 4 | 4 |
| 0101 | 5 | 5 |
| 0110 | 6 | 6 |
| 0111 | 7 | 7 |
| 1000 | 8 | 8 |
| 1001 | 9 | 9 |
| 1010 | 10 | A |
| 1011 | 11 | B |
| 1100 | 12 | C |
| 1101 | 13 | D |
| 1110 | 14 | E |
| 1111 | 15 | F |

_Every 4-bit pattern maps to one hex symbol. 1010 is A, 1111 is F. This table is the whole conversion: there is nothing else to memorize._

> **KEY:** To turn binary into hex, **group the bits into nibbles from the right** and replace each nibble with its symbol. `1010 0101` becomes `A5`. To go the other way, expand each hex digit back into its four bits. A byte is always exactly **two** hex digits, which is why bytes are almost always written in hex.

Because plain digits like `10` are ambiguous (is that ten, or the bits one-zero, or hex sixteen?), hex is usually marked. This course writes hex with a `0x` prefix, the convention from C and most tools: `0x2A` is hex, `42` is decimal, `0010 1010` is binary, and all three are the same number.

```
0x2A = 2*16 + 10 = 42 (decimal) = 0010 1010 (binary)
```

**Q (Try it):** Convert the byte `1100 1110` to hex, then to decimal.

**A:** Split into nibbles: `1100` and `1110`. From the table, `1100` is `C` and `1110` is `E`, so the byte is `0xCE`. In decimal that is `12*16 + 14 = 206`.

## Why this matters for circuits

Every value you will push around in the processor lessons (a byte on the [bus](https://digiwleea.wleeaf.dev/learn/buses/), an [instruction](https://digiwleea.wleeaf.dev/learn/machine-code/), a [memory](https://digiwleea.wleeaf.dev/learn/ram/) address) is some bits, and you will read them in hex constantly. A program that loads from address `0x0E` and adds the byte `0x05` is far easier to follow than one talking about `00001110` and `00000101`. Hex is the everyday handwriting of digital systems.

**Q (Predict, then check):** What hex digit does the nibble `1011` spell? Predict it, then set those bits in the figure below and confirm the pattern on the probes.

**A:** `1011` is `8 + 2 + 1 = 11`, which is hex `B`. In the lab, switch on `b3`, `b1`, and `b0` (leave `b2` off); the probes read `1 0 1 1`, the bits behind `0xB`.

_Circuit diagram: One hex digit is exactly one nibble. Set the four bits b3-b0 and the pattern they form is a single hex symbol (0-9, then A-F). Open it in the lab, dial in a few nibbles, and name the hex digit each one spells._

> **KEY:** You can now read and write numbers in binary and hex. Next we move from *numbers* to *logic*: the algebra of `0`/`1` and the laws (especially [De Morgan's](https://digiwleea.wleeaf.dev/learn/boolean-algebra/)) that let you reshape a logic expression, which is exactly what you do when you choose how to build a gate.

### FAQ

**Q:** What is hexadecimal?

**A:** Hexadecimal (hex) is the base-16 number system, using the digits `0`-`9` then the letters `A`-`F` for ten through fifteen. It is a compact, readable shorthand for bits: it does not change how the computer counts, it just lets us write the same bits more tidily.

**Q:** How do you convert binary to hexadecimal?

**A:** Group the bits into nibbles (4 bits each) starting from the right, then replace each nibble with its single hex symbol. Because `16 = 2^4`, one hex digit is exactly one [nibble](https://digiwleea.wleeaf.dev/learn/binary/), so it is a pure lookup with no arithmetic: `1010 0101` becomes `A5`.

**Q:** Why is hexadecimal used instead of binary?

**A:** Hex is used because long binary strings are painful to read, copy, or say aloud, and hex packs four bits into one symbol. A byte is always exactly **two** hex digits, so values on a [bus](https://digiwleea.wleeaf.dev/learn/buses/), an [instruction](https://digiwleea.wleeaf.dev/learn/machine-code/), or a [memory](https://digiwleea.wleeaf.dev/learn/ram/) address are far easier to follow in hex.

**Q:** What is the difference between binary and hexadecimal?

**A:** Binary is base 2 (digits `0` and `1`, one digit per bit) and hexadecimal is base 16 (digits `0`-`9` and `A`-`F`, one digit per four bits). They describe the **same** value: `0010 1010` in binary, `0x2A` in hex, and `42` in decimal are all the same number. Hex is written with a `0x` prefix here to avoid ambiguity.
