DYSTOPIANPIXEL

Number systems

Learn why decimal, binary and hexadecimal appear together, with small conversions you can check.

Next Asset Lab concept artwork showing sprite patterns, palette colours and animation frames
Next Asset Lab concept artwork · Begin with Odin Dev.2k and the foundations of Z80 assembly.
TUTORIAL 1 / PART 03

Why computers use different number systems

Start with the number system you already know: decimal

Most of us learn to count in decimal. Decimal is base 10, which means that it has ten digits:

0 1 2 3 4 5 6 7 8 9

When we run out of single digits, we add another position. The value of a decimal number depends on the position of each digit. For example, 347 means:

3 hundreds  = 3 × 100
4 tens      = 4 × 10
7 units     = 7 × 1

347 = 300 + 40 + 7

This idea of place value is the key to understanding binary and hexadecimal. The only thing that changes is the base.

Binary: base 2

A digital computer is built from circuits that naturally represent two states. We can think of those states as off/on, false/true, low/high, or simply 0 and 1. Binary is therefore base 2 and uses only two digits:

0 1

In binary, each position is worth twice the position to its right. Reading from right to left, the place values are:

... 128 64 32 16 8 4 2 1

Consider the eight-bit binary value:

%01000010

The 1 bits are in the 64 and 2 positions:

0×128 + 1×64 + 0×32 + 0×16 + 0×8 + 0×4 + 1×2 + 0×1
= 64 + 2
= 66 decimal

The percent sign is commonly used by assemblers to make it obvious that a number is binary. We will verify Odin syntax when we need to enter binary constants; for now, use the notation as a visual aid.

Hexadecimal: base 16

Hexadecimal, usually shortened to 'hex', is base 16. It needs sixteen digits. Decimal already gives us ten symbols, 0 to 9, so hexadecimal continues with the letters A to F:

0 1 2 3 4 5 6 7 8 9 A B C D E F

The letters are simply digits. In hexadecimal, A means ten, B means eleven, and so on up to F, which means fifteen.

Hex digitDecimal value
A10
B11
C12
D13
E14
F15

In the Odin source we have been using a dollar sign to identify hexadecimal numbers. Therefore $42 means '42 in hexadecimal', not forty-two in decimal.

Converting hexadecimal to decimal by hand

You do not need to memorise every hexadecimal value. For a two-digit hex number, multiply the left digit by 16 and add the right digit.

$2A

2 × 16 = 32
A      = 10

32 + 10 = 42 decimal

Another example:

$42

4 × 16 = 64
2      = 2

64 + 2 = 66 decimal

Converting a small decimal value to hexadecimal

For values up to 255, divide by 16. The whole-number result gives the high hex digit and the remainder gives the low hex digit.

42 decimal

42 ÷ 16 = 2 remainder 10
2  -> hex digit 2
10 -> hex digit A

42 decimal = $2A

At first it is perfectly reasonable to use a conversion table or calculator. The aim is to understand the relationship; speed comes naturally with practice.

Why hexadecimal is so useful in assembly language

You are correct to expect a lot of hexadecimal in assembly programming. It is not used simply to make the code look technical. It is used because hexadecimal fits binary extremely neatly.

One hexadecimal digit represents exactly four binary bits. Two hexadecimal digits therefore represent exactly eight bits, which is one byte. Four hexadecimal digits represent sixteen bits, which is the size of a normal Z80 address.

1 hex digit  = 4 bits
2 hex digits = 8 bits  = 1 byte
4 hex digits = 16 bits = 2 bytes

For example:

binary:      0100 0010
              4    2
hex:         $42
decimal:      66

This makes hexadecimal an excellent compact way to write values that are fundamentally binary.

A complete nibble conversion table

Because one hexadecimal digit corresponds to four bits, it is worth becoming comfortable with the following table. You do not need to memorise it immediately. Repeated use will make it familiar.

BinaryHex
00000
00011
00102
00113
01004
01015
01106
01117
10008
10019
1010A
1011B
1100C
1101D
1110E
1111F