Bytes, hex and bit width
Bits in fours and eights, hex as a shorthand for nibbles, and what happens past the biggest value.
Bits come in groups
Nobody handles bits one at a time. A single bit answers a single yes-or-no question, and almost everything a circuit does needs more than that, so bits are dealt with in fixed-size groups. Two sizes come up so often that they have names.
The byte is the group that matters most. Almost every computer built since the 1970s stores its memory as a long row of bytes, and bigger values are made of several bytes side by side. The nibble matters for a different reason: it is exactly the size of one hexadecimal digit, which is the subject of the next section.
Hex: one digit per nibble
Binary is easy for a circuit and tiresome for a person. Try reading 1011011010011100 out loud and you will lose your place halfway through. Hexadecimal, or hex for short, is a shorthand for writing bit patterns. It uses sixteen digits: 0 to 9 as usual, then the letters A to F for ten to fifteen. Sixteen is 24, so one hex digit stands for exactly one nibble, and a byte is always exactly two hex digits. Here is the whole table.
| Nibble | 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 |
To turn a byte into hex, split it into its two nibbles and look each one up. To go back, replace each hex digit with its nibble. There is no arithmetic in either direction, only the lookup, and after a little practice the table is in your head. Try it below: each nibble has its own hex digit, and flipping a bit only ever changes the digit it belongs to.
Worked example. Write the byte 10110110 in hex, then in decimal.
Split it into nibbles: 1011 and 0110. From the table, 1011 is eleven, which is B, and 0110 is six. So the byte is B6 in hex. For the decimal value, the left digit is worth sixteen times the right one, just as the left digit of a two-digit decimal number is worth ten times the right one: 11 × 16 + 6 = 176 + 6 = 182.
Why?: why hex and not decimal?
Decimal is the system you know, so why not write a byte as a number from 0 to 255? Because a decimal digit does not line up with any group of bits. Ten is not a power of two, so the decimal digits of 182 tell you nothing about which bits are set, and changing one bit can change every decimal digit. A hex digit is a nibble, so from B6 you can read off the bits directly. Hex is also short: sixteen bits become four characters, and a thirty-two bit value becomes eight.
Common mistake: reading hex 10 as ten
The same trap as binary, one lesson on. In hex, 10 is sixteen and FF is 255. When it matters which system a number is in, programmers put 0x in front of hex and 0b in front of binary, so 0x10 is sixteen and 0b10 is two. Also watch the split: nibbles are counted from the right, so a pattern that is not a multiple of four bits long gets its short nibble on the left.
Width: how many bits there are
The previous lesson introduced the width of a circuit: the fixed number of bits it holds. A width is nearly always a whole number of bytes, and four widths do most of the work in the world: 8 bits, 16 bits, 32 bits and 64 bits. An eight bit width holds one byte, and a sixty-four bit width holds eight of them.
The width sets the biggest number that fits. With n bits there are 2n patterns, one of which is zero, so the biggest value is 2n − 1. For a byte that is 255. For sixteen bits it is 65,535, and for thirty-two bits it is a little over four billion. Click every bit on below and read the value.
Now imagine adding one to all-ones. In decimal, 999 plus one needs a fourth column. In binary, 11111111 plus one needs a ninth bit, and an eight bit circuit does not have one. The 1 that should have gone into that ninth column has nowhere to go, so the eight bits that remain read 00000000. The value has wrapped round to zero. Nothing broke; the circuit did exactly what its wires allow. When you build an adder in stage 5 you will see that lost 1 come out on a wire of its own, called the carry out, so that a circuit which cares can catch it.
What to remember
- A nibble is 4 bits and a byte is 8. A byte holds 256 patterns, so 0 to 255 as an unsigned number.
- Hex uses the digits 0 to 9 and A to F. One hex digit is one nibble, so a byte is two hex digits.
- Converting between hex and binary is a lookup, one nibble at a time, with no arithmetic.
- Common widths are 8, 16, 32 and 64 bits. With n bits the biggest value is 2n − 1.
- Adding one to all-ones wraps round to zero, and the lost carry comes out on its own wire in an adder.
Check yourself
Get 5 right in a row and the lesson is done. A wrong answer costs the run, not the lesson.
A byte is written 20 in hex. What is it in decimal?
20
Already know this? and come back to the quiz any time.