How Computers Count, and How You Can Do Cool Stuff With The Same Techniques

8 minute read

(I'll get to that intriguing image later, don't worry. I can't put an image in the middle of my post, apparently.)

This is part one of an eventual series explaining a bit about how computers work internally. In this article, you will learn how to count to very high numbers on your fingers, and how to easily compact six yes/no values (do I need to bring x, y, and/or z home with me today?) into a single number for easy remembering. You'll also learn how this relates to the way computers work.

Hack 1: Counting to 1023 on your fingers
Ever needed to keep a quick tally of something but didn't have any paper handy? Counting on your fingers is a really easy way, but it's limited by the fact that we only have ten fingers (well, most of us, anyway). You could get clever and count to five on your right hand, then raise one of the fingers on your left hand to represent five. But even that system only gets you up to thirty.

The way I can get to 1023 (which is certainly more than you will ever need) is by using the binary number system instead of our usual (decimal) numbers. Here's a brief explanation of it, if you've never seen it:

In decimal, the second place represents ten of the first place. In other words, in the number 13, the 1 has ten times the value of the three. If we didn't know how the system worked, we could interpret the value of 13 by multiplying the 1 by ten, and the 3 by one, and adding them together: (1 * 10) + (3 * 1) = 10 + 3 = 13. Each new place multiplies the value of the previous place by ten, so we have the hundreds (10 * 10) place, then the thousands place, and so on.

In the binary system, the second place represents two of the first place. So the binary number 10 can be converted to a more familiar decimal by multiplying the twos place by two, and the ones place by one: (1 * 2) + (0 * 1) = 2 + 0 = 2. Each new place doubles the value of the previous place.

More concisely, the values of the places in the two systems are:
Decimal: thousands, hundreds, tens, ones
Binary: eights, fours, twos, ones

Obviously, binary is a lot less compact, since it only has two possible values for each place, 0 and 1. However, it has some great advantages as well: The 0 and 1 can just as easily represent off and on, which gives it the potential to work for both of these tricks.

To count to 1023 on your fingers, use your rightmost finger as the ones place, and keep working on up. That little picture at the top of this post shows the values that you would assign each finger (obviously you probably won't have the diagram when you need to use this method, but each finger is just twice the value of the one before it, so you can calculate it in your head fairly easily).

To count to, say, four, start by raising the 1 finger. Then lower it and raise the 2 finger. Then raise the 1 finger again. Then put down both the 2 and 1 fingers and raise the 4 finger. Obviously, this process can be continued indefinitely.

Hack 2: Storing Multiple Numbers in One Number, or How To Know What Classes You Have Homework In
I have a problem: During a typical day at school, I forget what classes I need to bring books home for. This results in wasting time at my locker (which is bad when I have a bus to catch) while I try to read my planner and see what I wrote down. And of course I can also forget to write something down thinking I'm sure to remember it.

I tried to solve this by spending a few moments reading my planner and thinking over everything I was supposed to do before I left my last class. But I often forgot in the few minutes between there and my locker. Then I had an idea.

I have six classes that I might need to keep track of. I have more than six fingers. So I assigned a finger to each class (in the order that I go to them during the day). If you add up the numbers for those fingers using the diagram, you'll get a unique number representing that permutation. A single number is a heck of a lot easier to memorize than yes/no values for six pieces of information. You can even use some fancy mnemonic system (like the Major System) if you need to remember the number for a while.

Of course, the entire system is useless if you can't convert the number back into the information you were trying to memorize. But that's easy enough.

1. Find the highest power of two that is less than the number you memorized (for instance, if my number is 58, the qualifying number is 32, as the next one, 64, is too high).
2. Raise the finger representing that number, and subtract the number from your memorized number.
3. Repeat until you hit 0.
4. Use the arrangement of fingers to reproduce the original information.

Does this seem awfully complicated? Yes, it does. How about an example?

My classes are:
Calculus
World Lit
U.S. History
Speech
German
Physics

Each of these gets a number, starting from the top.
Calculus (32)
World Lit (16)
U.S. History (8)
Speech (4)
German (2)
Physics (1)

Today I had homework in calculus, world lit, history, and German. So 32 + 16 + 8 + 2 = 58. 58 is "sushi" in my mnemonic system, so that's all I have to remember to know what books I need. Of course, I can start with 32 after my first class (or, preferably, 0, if I don't have any homework) and add to it as the day progresses, to keep a running total.

When I need to know what books I need to bring, I take 58, raise my left thumb (which represents 32), then subtract 32 from 58, leaving me with 26. Then I raise my right thumb and subtract 16, leaving me with 10. And so on. The math only takes a few seconds.

Naturally, mine is not the only application of this system. There could be numerous more situations like it, and maybe you can think of one.

What The Heck This Has To Do With Computers
This article is only partly about a few silly (though occasionally useful, at least to me) tricks--it's a computer newsletter, and needs to be at least tangentially related.

Binary is the system that a computer uses internally to represent numbers (and everything else). If you wrote a program that simply counted up to infinity (or rather, until the computer ran out of memory), it would internally do something almost exactly like my finger-counting process, the only difference being that the changes are electrical charges moving on a microscopic scale rather than fingers. (Adding and manipulating those numbers is quite another matter, and while it's quite interesting, it's not well-suited for an easy-reading newsletter.)

As for the other hack, it relates to a common programming trick. When you define an integer to store a number (if it's been too long since algebra, an integer is a positive or negative number with no decimal places), the computer sets aside a certain number of binary bits, typically 32, to store the number. (A bit is either a 0 or a 1, a single binary digit; a binary 1 would be one bit, while a binary 1010 would be four bits.) This way, it can store any number between 0 and 2147483647. (Lost in the technical talk? Jump down to the next paragraph.)

All this is to say that if you store a value of 1 (which uses only one binary bit), you're using thirty-two times the amount of memory that you need to. That doesn't sound particularly significant, but when you start storing thousands or millions of pieces of information in a database or other file, you want to conserve as much space as possible.

So instead you can use a method similar to what I described above. The computer uses a different process involving binary numbers and Boolean logic, which is technical enough that I'll spare you from it, but the basic idea is the same--pack a bunch of different numbers (or, in my case, yes/no situations represented by ones and zeroes) into a single number.

(Phew! I'll have something a bit simpler for you next week, but I hope you learned something from this.)

--
Soren "scorchgeek" Bjornstad
http://thetechnicalgeekery.com

If you have found an error or notable omission in this tip, please leave a comment or email me: webmaster@thetechnicalgeekery.com.

Copyright 2011 Soren Bjornstad.
Verbatim copying and redistribution of part or all of this article
is permitted, provided this notice is preserved.