George V. Reilly

A Use for Octal: Calculating Modulo 36 from Modulo 9

(I posted an earlier version of this in December 2004 on my old technical blog. A discussion at work last week about 36-bit computers at the Living Computers Museum prompted me to write an updated post with improved ex­pla­na­tions and much better typography.)

I've been pro­gram­ming in C since 1985 and C++ since 1991, but I've never found a use for octal rep­re­sen­ta­tion until [2004], aside from the per­mis­sions argument for chmod. Octal has always seemed as vestigial as a human appendix, a leftover from the early days of computers, when word sizes were often a multiple of three: 6-, 12-, 24-, or 36-bits wide. All modern computers use word continue.

0xFF...FF / 10 == 0x19...99

A few weeks ago, I wrote a C++ routine to parse decimal numbers using the overflow detection principles of SafeInt. I couldn't find anything in the libraries that actually did a good job of checking for overflow.

Briefly, to see if unsigned values A+B overflow, check if (A > MAX_UINT - B). Similarly, A*B will overflow if (A > MAX_UINT / B).

// Convert a string to an unsigned. Returns 'true' iff conversion is legitimate.
    const string& str,
    unsigned&     rUint)
    rUint = 0;

    if (str.empty())