Counting in Twos and Sixteens
Last timeEverything Is a Run of Bytes
Place value works in any base. Two is what the hardware counts in, sixteen is what people read it in, and a byte dump is legible once the two are the same idea.
Place value in any base
The number four hundred and seven, written in the usual way, means four hundreds, no tens and seven ones. The columns are worth one, ten, a hundred, a thousand, each ten times the one to its right. That multiplier is the base, and nothing else about the system depends on it being ten.
Put two in instead, and the columns are worth one, two, four, eight, sixteen. Each column holds either none or one of its value, because those are the only digits available. Put sixteen in, and the columns are worth one, sixteen, two hundred and fifty six, and each column holds anything from none to fifteen of its value.
$$
Here is the base, is the digit in column counting from the right starting at zero, and is the value. The sum is the whole of place value, and the three bases in this lesson are the same expression with two, ten and sixteen put in for .
| in tens | columns used in twos | in sixteens, as two digits | |
|---|---|---|---|
| nine | 9 | 4 | 9 |
| ten | 10 | 4 | 10 |
| sixteen | 16 | 5 | 10 |
| sixty four | 64 | 7 | 40 |
| two hundred and fifty five | 255 | 8 | 255 |
| two hundred and fifty six | 256 | 9 | 256 |
Reading sixteens
A value written in sixteens needs sixteen digits and the familiar set runs out at nine, so the next six are written as the letters A to F standing for ten to fifteen. That is a typographic convenience, chosen so each digit remains one character wide, and it carries no mathematical content at all.
Reading a value written this way is the sum from above with sixteen put in. The three digit value written 1F4 is one lot of two hundred and fifty six, plus fifteen lots of sixteen, plus four, which is five hundred.
| step | as written | the columns | the arithmetic | in tens |
|---|---|---|---|---|
| 1 | 0A | no sixteens and ten ones | 10 | 10 |
| 2 | 20 | two sixteens and no ones | 2 times 16 | 32 |
| 3 | FF | fifteen sixteens and fifteen ones | 15 times 16 plus 15 | 255 |
| 4 | 1F4 | one 256, fifteen 16s and four ones | 256 plus 240 plus 4 | 500 |
Why sixteen and not ten
The reason for sixteen rather than the familiar ten is that the boundaries line up. Sixteen is two multiplied by itself four times, so one digit in sixteen is exactly four binary switches, and one byte of eight switches is exactly two digits with nothing left over.
That makes the translation a lookup. Each group of four switches maps to one character and back, with no carrying and no arithmetic, and a person who has done it a few times reads a dump without consciously converting anything.
Base ten does not line up with anything. Two hundred and fifty five is three digits, sixteen is two, and a byte boundary falls in the middle of a digit, so a dump written in tens is unreadable as a layout even though every value in it is correct.
The lesson stops here
2 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents