QIAOPANG.COM · SELECTED ESSAYS (2013–2026) · 260 ESSAYSBrowse Chronologically (2013 → 2026) ↗
QiaoPang · Programmers LifeSelected Essays · Systems, Craft & Agentic Era
Timeline ↗

Programmer's Christmas

Programmer's Christmas

Today is not just Halloween; 31 OCT is also 25 DEC, so it’s also Programmer’s Christmas.

…

Okay, OCT stands for octal. Octal 31 is decimal 25, and decimal is DEC. Yep, a standard programmer’s dry joke; if you take it seriously, you lose.

(I can almost hear Guo Degang ringing in my ears: “Are you sick? Do you have medicine?”)

Number bases are fascinating. We should be grateful that humans evolved with five fingers. If we had seven, our everyday base would be base-14, and elementary school math would most likely become much harder—because we’d have to memorize a 13×13 multiplication table.

In daily life, apart from the overwhelmingly dominant decimal, we also use duodecimal (buying socks, eggs, and the like by the dozen), sexagesimal (timekeeping and angular minutes and seconds), base-360 (angles), and base-365 (counting days). Some cultures use base-20—for example, insisting on saying 84 as four twenties and a four, making trouble for themselves. In a programmer’s life, decimal is not mainstream; binary, hexadecimal, and octal reign supreme. The derived base-64—twenty-six uppercase and lowercase letters, ten digits, plus two symbols—also appears openly in mainstream circles. Later, to solve the confusion between I, l, O, and 0, Bitcoin derived base-58 from base-64.

When we discuss number bases—binary, say—it carries one every two, so starting from the lowest digit, going upward we have $2^1$, $2^2$, and so on. For a base-x number, we reason by analogy:

$$ \begin{array}{l} \text{decimal}:&8493&=8\times10^3+4\times10^2+9\times10^1+3\\text{binary}:&1101&=1\times2^3+1\times2^2+0\times2^1+1\\text{base }x:&3409&=3x^3+4x^2+0x^1+9 &\text{assuming }x>9 \end{array} $$

This is interesting: a polynomial actually represents a number in an unknown base! So can we express polynomial multiplication and division using the decimal arithmetic we are used to?

For example, how do we divide $x^3+3x^2+5x+15$ by $x+3$? In middle and high school, we need to factor the polynomial, and factoring is not easy. But if we treat it as a decimal number, it is 135(15), that is, 1365; divide by 13 and we get 105; then convert the decimal 105 back to base x, and we get $x^2+5$. Perfect. Multiplication works the same way—just be careful to write carries in the form 135(15), not to carry them upward.

You may ask: what about dividing a polynomial containing negatives, like $x^2-1$, by $x-1$? Viewed as decimal, $x^2-1$ is (100-1), and $x-1$ is (10-1); the quotient is 11, which converts back to $x+1$. Easily done! Note that here there is borrowing and repaying. For example, $x-1$ times $x^3+x^2+x+1$ is (10-1) times 1111, giving 9999; we need to restore it to (10000-1), that is, $x^4-1$.

Since the carry system can be 2, 3, 5, 10, 60, 365, can it be 0.5? Ha, a carry of 0.5 means the ones place is set to one, the tens place carries two, then the hundreds place carries four, continuing infinitely. This is a strange carry system; any number becomes endless.

What about base 1.5? That is, carry two when you reach three? In base 1.5, 20 represents decimal 3, and 2100 represents decimal 9. This base, written as an expression, is $2\times1.5^3+1.5^2$.

So in a base where you carry y every x, a number 1234 represents $(x/y)^3+2\times(x/y)^2+3\times(x/y)+4$. Very nice, very bizarre. What use is it? Apparently none. That is how things are in mathematics—like Zhuangzi’s “the usefulness of the useless”: “Now you have a giant tree and worry that it is useless. Why not plant it in the land of Nothing-Whatsoever, in the wilds of the boundless?”

I have rambled on about this and that. Some of it came from Xiaobao’s silly questions when I was teaching her math; some came from videos I watched online and mulled over. Programmer’s Christmas should not involve this sort of thing—it hurts the brain. Better to show you ten photos I took tonight at Halloween:

MORE IN Essays & Reading

Further Reading in This Column

View all essays in “Essays & Reading” →

Happy New Year!

In the blink of an eye, a twelfth of 2020 is gone — has your New Year plan kept pace? After a month of outrage over attacks on doctors and anxiety over Wuhan's outbreak, I send New Year wishes along with a guide to a long holiday without regret: books, Rust, and time with the kids.

Book Next Chronicles (2013–2020)3,608 chars · 8 min read