Skip to content

Four Bits at a Time

Hex gives each half of a byte a single symbol, so the writing still shows you the switches.

You can now write a number in switches. The trouble is reading one back.

11111111 — quick, how many ones? You had to count. Binary tells the whole truth, but eight switches is more than you can take in at a glance, and programmers look at these all day. They wanted a shorthand that still showed the bits. That shorthand is hexadecimal.

Decimal hides the bits

Why not read the byte in ordinary tens? Because decimal does not line up with switches.

11111111 is 255. That tells you nothing about which switches are on. Turn one off and you get 254. The decimal barely moves, while the bit pattern changed in a clear, specific place.

We want a shorthand where the writing still shows the switches. Tens cannot do that. Fours can.

Group the bits in fours

Split a byte down the middle into two halves of four bits. Four bits has its own name. It is a nibble, which is half a byte.

How high does one nibble count? Turn all four on:

SHOW BIN 1111
Output
15

A nibble runs 0 to 15, which is sixteen values. That is the whole idea of hex: give each nibble a single symbol, so four bits become one character.

Sixteen needs sixteen symbols

Here is the snag. We have ten digits, 0 to 9. Sixteen values need sixteen symbols, so hex borrows the first six letters for the values past nine. A is ten, B is eleven, and so on up to F for fifteen.

Bits Decimal Hex
0000 0 0
0101 5 5
1001 9 9
1010 10 A
1100 12 C
1111 15 F

So A to F are not letters here. They are digits, the ones we ran out of symbols for. F means a full nibble, all four on.

A byte is two hex digits

Two nibbles make a byte, so a byte is exactly two hex digits. Write HEX in front of them the way you write BIN in front of switches:

SHOW BIN 11111111
SHOW HEX FF
Output
255
255

Both lines came to the same number, because they are the same number written two ways. F is the high nibble and F is the low one.

Now watch what hex shows that decimal hid. Turn the low nibble off and leave the high one on:

SHOW BIN 11110000
SHOW HEX F0
Output
240
240

In decimal, 255 became 240. That jump hides what happened. In hex it went FF to F0, and the change is right there in the second digit. The low nibble emptied.

That is hex’s whole appeal. Each digit is one nibble, so you read the bits straight off.

Why programmers reach for it

  • It lines up with bits. One hex digit is exactly four switches.
  • It is short. A byte is two characters instead of eight.
  • It is everywhere. Memory addresses, colour values and the bytes of machine code are almost always written in hex, because hex shows the bits.

Not every language writes it the way we have. Most use a marker in front of the digits instead of a word, and $F0 and 0xF0 are the two you will meet most.

When it’s wrong, see why

  • A hex digit above F. There is not one. Hex runs 0–9 then A–F and stops. A “G” is not a hex digit.
  • Reading hex 10 as ten. It is one full nibble past F, which is sixteen. A leading hex digit counts in sixteens. That is exactly why the base gets marked, by a word like HEX or a sign like $.
  • Mixing up the halves. The left hex digit is the high nibble, the right is the low one. Big on the left, as everywhere else.

What you’ve learnt

  • Hex is base sixteen: one symbol per nibble, so four bits become one character.
  • Counting runs 0–9 then A–F, and the letters are the digits past nine.
  • A byte is two hex digits, 00 to FF. FF is 255, the all-on byte from Unit 1.
  • Hex earns its keep because it shows the bits where decimal hides them.

What’s next

A byte runs out at 255, and a game needs bigger numbers than that before the first level is over. In Unit 3 we put two bytes side by side, and hex turns out to have been ready for it.