NCL 207: Number Bases

Earlier in the 200 level, we saw that values in memory are stored as strings and interpreted by the instruction that uses them.

This applies to numbers too.

MOVE $000 255

stores:

255

The number itself is two hundred and fifty-five. "255" is one way of representing that number as text.

It isn't the only one.

Decimal

The number system we normally use is decimal, or base 10.

In decimal, each position represents a power of ten.

Consider 1234:

Position 1000 100 10 1
Digit 1 2 3 4
Value 1000 200 30 4

Each value in the bottom row is the digit multiplied by its position.

Add those values together:

1000 + 200 + 30 + 4 = 1234

The position of each digit determines its value. This is positional notation.

There is nothing special about using ten as the base.

Binary

You've probably heard of binary.

It's computer stuff. Ones and zeroes.

Binary is base 2, so we have two digits: 0 and 1.

Counting starts normally enough with 0, 1—and then, whoops, 2 doesn't exist.

We carry into the next position instead.

The same positional rule still applies, except each position is now a power of two.

Consider 1010₂:

Position 8 4 2 1
Digit 1 0 1 0
Value 8 0 2 0

Add the bottom row:

8 + 0 + 2 + 0 = 10

So:

1010₂ = 10₁₀

This gives us an important rule:

10 does not inherently mean ten.

In decimal, 10₁₀ is ten. In binary, 10₂ is two.

The digits tell us how many of each position we have. The base tells us what those positions are worth.

Octal

Octal, or base 8, is basically decimal if you're missing two fingers.

Its digits run from 0 through 7. After 7, we carry into the next position, so 10₈ represents decimal 8.

Its positions are powers of eight.

Consider 1234₈:

Position 512 64 8 1
Digit 1 2 3 4
Value 512 128 24 4

Add the bottom row:

512 + 128 + 24 + 4 = 668

So:

1234₈ = 668₁₀

Each octal digit corresponds neatly to three binary digits. Octal is less common in modern programming than binary or hexadecimal, but you'll still encounter it occasionally.

Why does anyone use octal?

Octal is a compact way to write binary values.

One octal digit corresponds exactly to three binary digits:

0₈ = 000₂
1₈ = 001₂
...
7₈ = 111₂

That made octal particularly convenient on computers whose word sizes divided naturally into groups of three bits.

Hexadecimal later became more common for many uses because one hexadecimal digit corresponds to four bits, but octal never disappeared entirely.

Hexadecimal

Hexadecimal is base 16.

We can count normally through 0–9, then hold on—we're out of digits here.

We need six more before we can carry into the next position, so hexadecimal borrows the letters A through F:

Hexadecimal Decimal
A 10
B 11
C 12
D 13
E 14
F 15

After F, we finally carry, so 10₁₆ represents decimal 16.

Its positions are powers of sixteen.

Consider 1234₁₆:

Position 4096 256 16 1
Digit 1 2 3 4
Value 4096 512 48 4

Add the bottom row:

4096 + 512 + 48 + 4 = 4660

So:

1234₁₆ = 4660₁₀

Each hexadecimal digit corresponds neatly to four binary digits, which makes hexadecimal especially convenient when working with binary values.

Comparing the bases

The same number can be represented in any of these bases.

Decimal Binary Octal Hexadecimal
0 0 0 0
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
16 10000 20 10
32 100000 40 20
64 1000000 100 40
128 10000000 200 80
256 100000000 400 100

Notice what happens to 100:

100₂  = 4
100₈  = 64
100₁₀ = 100
100₁₆ = 256

The digits are the same. The base determines what their positions mean.

The table also shows a useful pattern around powers of two.

Since 100₁₆ is decimal 256, one less is:

FF₁₆ = 255₁₀

Just as 99 is one less than 100 in decimal, FF is one less than 100 in hexadecimal.

Binary follows the same pattern:

11111111₂ = 255₁₀

You may already have encountered hexadecimal in RGB colour codes such as #FF8000.

Each pair of hexadecimal digits represents one colour component, ranging from 00 through FF—or decimal 0 through 255.

Number bases in NCL

NCL integer literals can be written in all four bases:

Base Prefix Example
Binary 0b 0b11111111
Octal 0o 0o377
Decimal — 255
Hexadecimal 0x 0xFF

These instructions are equivalent:

MOVE $000 255
MOVE $000 0b11111111
MOVE $000 0o377
MOVE $000 0xFF

Each stores:

255

Binary, octal, and hexadecimal literals are converted to decimal during the program pre-pass. Their source notation is not preserved in memory.

Converting between bases

Sometimes we want the representation itself.

The String Coprocessor provides four base-conversion instructions:

S.BIN <dst> <value> [base]
S.OCT <dst> <value> [base]
S.DEC <dst> <value> [base]
S.HEX <dst> <value> [base]

The instruction determines the output base. If base is omitted, the input is interpreted as decimal.

S.BIN $000 255
S.OCT $001 255
S.DEC $002 255
S.HEX $003 255

produces:

$000 = "11111111"
$001 = "377"
$002 = "255"
$003 = "FF"

The results are ordinary strings without a base prefix.

The optional base operand lets us interpret input in another base:

S.DEC $000 "FF" 16
-- > $000 = "255"

S.HEX $001 "11111111" 2
-- > $001 = "FF"

S.BIN $002 "377" 8
-- > $002 = "11111111"

This lets us convert directly between bases without first producing decimal text.

Formatting the result

Because the results are ordinary strings, we can use the string operations we've already learned to format them.

For example:

S.HEX $000 10
S.PADL $000 $000 "0" 2
-- > $000 = "0A"

Or in binary:

S.BIN $000 10
S.PADL $000 $000 "0" 8
-- > $000 = "00001010"

We can also add a prefix ourselves:

S.HEX $000 255
S.PADL $000 $000 "0" 4
S.JOIN $000 "0x" $000
-- > $000 = "0x00FF"

This time, "0x00FF" actually exists in memory.

An integer operation can interpret it:

MOVE $001 $000
-- > $001 = "255"

Compare that with:

MOVE $001 0x00FF

Here, 0x00FF is part of the NCL source and is converted during the program pre-pass.

Both arrive at the same numeric value by different routes.

Values and representations

These all represent the same number:

11111111₂ = 377₈ = 255₁₀ = FF₁₆

As strings, however:

"11111111"
"377"
"255"
"FF"

they are different sequences of characters.

NCL can interpret those strings as numbers, convert between representations, or manipulate the representations as ordinary text.

Try it

Let's make a small hexadecimal byte display.

Start with:

#value 10
#hex $000

Convert #value to hexadecimal and pad the result to two characters.

For 10, the result should be:

0A

Add a 0x prefix to produce:

0x0A

Then display the result.

Try changing #value to:

0
15
16
127
255

The hexadecimal field should always contain exactly two digits.

Finally, use the completed numeric string as the source of an integer MOVE and display that result in decimal.

For 255, your program should be able to go from:

255

to:

0xFF

and back to:

255

A number and the text used to represent it are related, but they are not the same thing.

NCL can interpret several representations directly in source, while the String Coprocessor lets programs convert and format those representations at runtime.

In NCL 208: Special Characters, we'll return to strings and look at characters that are difficult to write directly, including characters that can affect how text is displayed.