Skip to content

A Letter Is a Number

The machine holds nothing but numbers, so a letter is a number too. Which number is an agreement, and the agreement has a pattern in it.

A byte holds a number. That is all it can hold.

So when a program shows you the word SCORE, what is in memory? Not letters. There are no letters in there. There are numbers, and everyone has agreed which number means which letter.

The agreement

Each character gets a number, and that number is its code. Write CODE in front of a character to ask which one:

SHOW CODE "A"
Output
65

Capital A is 65. Go the other way with CHARACTER:

SHOW CHARACTER 66
Output
B

Nothing about 65 is naturally an A. Somebody decided. The common agreement is called ASCII, and most machines of this era followed it for the ordinary letters, digits and punctuation. They did not all agree beyond that, and the Commodore 64’s set differs from it in places you would notice.

That is worth holding on to. A character code is a convention, not a fact about the universe.

The codes run in order

The agreement was not made at random. A is 65, B is 66, C is 67, on through the alphabet in order.

That ordering is what lets a program sort names, or check whether a letter falls between two others. Comparing two letters is comparing two numbers.

The trap: a digit is not its value

Digits have codes too, and here is where people come unstuck:

SHOW CODE "7"
Output
55

The character "7" is 55, not 7.

They are different things. The number 7 is a quantity you can add. The character "7" is a shape on the screen that happens to look like a digit. Ask someone to type a number, treat their answer as characters, and your sums come out as nonsense.

The digits run in order too, from "0" at 48. So the value of a digit character is its code minus 48, which is how a program turns typing into arithmetic.

Upper and lower are one bit apart

Now look at what the agreement did with capitals. A is 65 and a is 97. The gap is 32.

It is the same gap for every letter, and 32 is not a round number by accident. Write both in binary:

SHOW BIN 1000001
SHOW BIN 1100001
Output
65
97

1000001 and 1100001. Look along them. Every bit is the same except one, and 32 is the place value of the bit that differs.

So changing case is not a lookup or a special rule. It is one switch:

SHOW CHARACTER (CODE "A" BITOR 32)
Output
a

BITOR 32 turns that switch on, which makes any capital lower case. Turning it off makes any lower-case letter a capital.

Whoever laid out ASCII chose the codes so that the case of a letter would be a single bit. That is the tool from Unit 1 doing real work, on a byte you have been reading all along.

When it’s wrong, see why

  • Your sums came out huge or strange. You are adding character codes rather than numbers. "7" is 55.
  • A comparison sorted oddly, with every capital before every lower-case letter. It did exactly what you asked. Z is 90 and a is 97, so by code, Z does come first.
  • The same file looked wrong on another machine. The agreement is not universal. Above the ordinary letters and digits, machines went their own ways.
  • BITOR 32 mangled something. It is a rule about letters. Applied to a digit or a punctuation mark it just changes the character to something else.

What you’ve learnt

  • A character is stored as a number, called its code.
  • Which number is an agreement, commonly ASCII, and machines did not all agree.
  • Letters and digits run in order, which is what makes sorting and comparing work.
  • The character "7" is not the number 7.
  • Upper and lower case differ by one bit, so BITOR 32 changes case.

What’s next

Every number so far has been a whole one. In Unit 4 we handle the in-between: half a pixel, and two-thirds speed.