Basis Lab

Bytes, Text and File Formats

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.

$v=∑idi Biv = \sum_{i} d_i \, B^{i}$

Here BB is the base, did_i is the digit in column ii counting from the right

starting at zero, and vv 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 BB.

FIG 1The same six values in three bases
in tenscolumns used in twosin sixteens, as two digi
nine949
ten10410
sixteen16510
sixty four64740
two hundred and fifty fi2558255
two hundred and fifty si2569256
The last column is the value a reader would see written in sixteens, shown here as ordinary digits because this figure holds numbers rather than text. Two rows are worth pausing on. Sixteen in base ten is written as one zero in base sixteen, which is why the third row shows ten: the one is a sixteen and the zero is no ones. And the last two rows show where a byte ends, since two hundred and fifty five uses eight switches and two hundred and fifty six needs a ninth.

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.

FIG 2Reading four values written in sixteens
stepas writtenthe columnsthe arithmeticin tens
10Ano sixteens and ten ones1010
220two sixteens and no ones2 times 1632
3FFfifteen sixteens and fifteen ones15 times 16 plus 15255
41F4one 256, fifteen 16s and four ones256 plus 240 plus 4500
4 steps
The leading zero in the first row is not decoration. Bytes are written as two digits by convention precisely so that a dump lines up in columns, and the third row shows why two digits is exactly right: the largest value a byte holds is written FF, and anything needing three digits is more than one byte. The prefix 0x, which appears in front of these values in most programs, is only a marker saying that what follows is written in sixteens.

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 contents

This 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

The rest of this course

  1. 01Everything Is a Run of Bytes
  2. 02Counting in Twos and Sixteensyou are here
  3. 03Letters as Numbersopening only
  4. 04Whole Numbers, and Where They Stopopening only
  5. 05Numbers With Fractions In Themopening only
  6. 06Dates, Times, and the Day It Movedopening only
  7. 07Rows, Columns and the Separator Problemopening only
  8. 08Making It Smalleropening only

Read alongside