In Praise of Octal

When you work with computers, it’s hard to avoid knowing something about the various numerical bases. We are accustomed to decimal math, of course, and that’s natural. We have ten fingers, ten toes, ten yards for a first down, ten millimeters in a centimeter, and so on. But computers have no particular fondness for decimal numbers. They work in binary (base two) numbers, and we need to endow them with special instructions and rules for handling binary representations of decimal integers and real numbers.

Binary numbers are a real pain for people to work with. For one thing, they’re not very compact. 233 is three cool decimal digits, but in binary the most compact way of representing it is 11101001, a full 8 bits. That’s considered a short integer, or a byte. Every character in this blog represents that many bits, so you can imagine how long this sentence would be if it were written out in binary.

Every digit in a decimal number represents a multiple of a power of 10. 635 decimal is (6 x 10^2) + (3 x 10^1) + (5 x 10^0).

The biggest problem is that there is no convenient relationship between binary and decimal. Before programmer’s calculators, the manual way of converting a decimal number to binary was to take advantage of the fact that each binary digit is a multiple of a power of 2. The multiple can be either 1 or 0. So you would write down the powers of two from right to left, then move from left to right asking yourself if that power of 2 fits into the decimal number. If so, put a 1 in that column, subtract the power of 2, and continue with the remainder. If not, put a zero in that column and continue without subtracting. So to convert 67:

2^72^62^52^42^32^22^12^0
DEC1286432168421
FITS?01000011
67673333310
Converting decimal to binary, the old fashioned way!

What a pain. So how can we represent these bytes more compactly? How about hexadecimal, or base 16? We can use 0-9, plus the letters A-F. Conveniently, one hexadecimal digit can be represented in exactly 4 bits. So if we write our numbers in hex, converting to binary is easier. F is all ones, A is 0111, 3 is 0011, etc.

25 hex = 0010 0101 (Just convert four bits at a time. Easier!)

Still, doing that in your head can be a pain. There are 16 possible comb8nations of bits in a hex digit, and without going through some math in your head, it’s hard to instantly remember that hex A is 1011 and hex 9 is 1001. It took me a few seconds to think about those.

Back in the dark ages, computers had front panels with switches. Sometimes starting the computer or booting it into a particular low-level program required toggling in an address on the switches. It was tough to stand there and read hex to set the switches; that was slow and cumbersome. There must be a quicker, easier way for the human mind to rapidly convert a fairly compact notation into binary, in his HEAD.

Enter octal, base 8. In octal, every digit can be represented as exactly THREE bits! There are only 8 possible combinations of 3 bits, and anyone can quickly memorize those eight. So on the front panel of a PDP-11 the plastic switch handles were arranged in groups of three — 3 light colored, then three dark, then three light again. Addresses or data to be entered were usually written in octal, and you would just go across the switches.

14773050 was easy. 001 100 111 111 011 000 101 000 … I converted that as fast as I could type it, thanks to knowing those combinations from years of front panel shenanigans.

Octal has fallen out of favor since the demise of the hardware front panel, but I still find it useful now and then, and so do many coders and hardware guys; it’s still available as a working base on many scientific and ALL programmer’s calculators. In fact, instant conversion between octal, hex, binary, and decimal is one reason the rare HP-16C calculator is so prized. It’s ability to convert bases, do complements, and perform bitwise logic operations make it extremely useful! (If you have an iPhone, check out “16C Scientific Calculator” on the App Store. It’s a faithful emulation!)

The awkward relationship between decimal and binary makes real number math inside a calculator or computer interesting. On paper, we can perform computations to any desired precision. But on a calculator the digits are finite, and even inside a computer the precision we can expect depends on how many bytes we are willing to use to store a real number. We have to compromise between the precision we want and the range of numbers we want to represent. This is where floating point arithmetic comes in: we store a signed mantissa (or significand) with the desired precision, and an exponent that represents a power of some base (typically 10). This sort of number is all kinds of fun to work with in binary, and that’s why to this day a computer’s speed is measured in flops … floating point operations per second. Today we hear more about megaflops, gigaflops, and even teraflops when supercomputers are described.

Well, that’s my bit about octal. Geezer mode off.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.