Glossary: hex, $ and byte order

The notation used in my guides, explained from scratch: what hexadecimal is, why addresses start with $, and why some numbers are stored back to front.

By Andrew · Last updated

The technical guides on this site, Inside the Sharp MZF file and What's in a ZX81 .P file?, are full of things like $4009 and 00 12. This page explains that notation. You don't need any maths beyond adding and multiplying.

Bits and bytes

A bit is a single 0 or 1. A byte is a group of 8 bits, so it can hold 256 different values, from 0 to 255. All the 8-bit computers on this site work on one byte at a time. That's what "8-bit" means.

Numbers bigger than 255 need more than one byte. Two bytes together (16 bits) can hold any value from 0 to 65,535. That's why these machines can address at most 64 KB of memory: every memory address is a 16-bit number.

Hexadecimal (hex)

We normally count in decimal, base 10. It uses ten digits, 0 to 9, and each column is worth ten times the one to its right: units, tens, hundreds.

Hexadecimal is base 16. It needs sixteen digits, so after 0–9 it borrows the letters A–F:

Hex0123456789ABCDEF
Decimal0123456789101112131415

Each hex column is worth sixteen times the one to its right. So the columns are worth 1, 16, 256 and 4,096.

Working one out

Take $1200, the address where Sharp MZ-80K programs usually load:

  1        2        0       0
  1×4096 + 2×256 +  0×16 +  0×1  =  4096 + 512  =  4608

And $4009, where a ZX81 .P file starts in memory:

  4×4096 + 0×256 + 0×16 + 9×1  =  16384 + 9  =  16393

Why bother with hex?

Because one hex digit stands for exactly 4 bits, so one byte is always exactly two hex digits, from 00 to FF (0 to 255). A 16-bit address is always four hex digits, from 0000 to FFFF. That makes hex very easy to read alongside memory:

The $ sign means "this is hex"

Is 10 ten or sixteen? On its own you can't tell, so programmers mark hex numbers with a prefix or suffix. In my guides a $ in front means hexadecimal: $10 is sixteen and 10 is ten. This convention comes from 6502 assembly language and was common on 8-bit home computers.

You'll meet other markers too, depending on the machine or language:

NotationWhere you'll see itExample (= 4608)
$ prefix6502 assembly, many 8-bit manuals, my guides$1200
& prefixBBC BASIC on the BBC Micro and Master&1200
# prefixAtom BASIC on the Acorn Atom#1200
h suffixZ80 assembly and Sharp/Zilog documentation1200h
0x prefixC, JavaScript, Python and most modern languages0x1200

They all mean the same thing. On the BBC Micro you can try it: PRINT &1200 prints 4608, and PRINT ~4608 prints it back in hex.

In hex dumps, such as the rows of bytes in my guides, the $ is usually left out, because everything in a dump is hex. 00 12 means the bytes $00 and $12.

Big-endian and little-endian

A 16-bit number takes two bytes. Take $1200. One byte is the high byte, $12, the "big end" worth 256 each. The other is the low byte, $00, the "little end" worth 1 each. When the number is stored in memory or in a file, which byte goes first?

Byte orderWhich byte comes first$1200 is stored asUsed by
Big-endianHigh byte first, the way we write numbers12 00Motorola 68000, network protocols, ZX81 line numbers
Little-endianLow byte first, "back to front"00 12Z80, 6502, and modern Intel and Apple Silicon processors

Neither is better. Each processor designer simply picked one. The Z80 and 6502 are both little-endian, so nearly every 16-bit value on the Sharp MZ, Sinclair and Acorn machines is stored low byte first.

Reading a little-endian number

To read a little-endian pair, swap the two bytes, then read them as one hex number. In the MZF header, the load address is stored as 00 12:

stored:   00 12
swapped:  12 00
value:    $1200 = 4608

A real file that uses both

The ZX81 is a good test, because one BASIC line uses both orders. The line number is big-endian, but the line length that follows it is little-endian, like everything else on the Z80:

00 0A   06 00
-----   -----
line    length
number  (little-endian: swap to $0006 = 6)
(big-endian: $000A = 10)

If you read the line number the Z80 way by mistake, you get $0A00, which is 2,560. That mistake is a good sign you've got the byte order the wrong way round. See What's in a ZX81 .P file? for the full example.

Where the names come from

The terms come from Jonathan Swift's Gulliver's Travels, in which two nations go to war over which end of a boiled egg to crack first. Computer scientist Danny Cohen borrowed them in 1980 to poke fun at how seriously engineers argued about byte order.

Number converter

Type a decimal number, or a hex number starting with $, &, # or 0x:

Decimal
Hex
Binary
Stored little-endian (Z80, 6502)
Stored big-endian

Comments

  1. Loading comments…

Leave a comment

Every comment is read by me before it appears. Please keep it friendly and on topic. Links and personal details are removed. See the comment policy.