All Your Bases are Belong to Us [From the Archives]

My “Day I Left Pennsylvania” led me to some archived website posts (before blogs were invented) I had written many years ago. I’m re-posting them now. Bear in mind that most of the content in this series is over 5 years old. I have left the content more or less intact. I have removed some links and added some others — but that’s it. Enjoy!


Non-decimal bases can be intimidating. Our brains are wired so tightly for doing base-10 (decimal) calculations, that thinking in terms of any other bases seems ridiculously counter-intuitive.

And yet we use them all the time.

Our clocks are base 12 (dodecimal). We count time in quasi-base 60 (sexagecimal). We count standard weight (ounces) in base 16 (hexadecimal). And as a child, if you ever used the tally system (|, ||, |||, ||||, ||||), then you were using base 5 (pental).

The Sumerians used base-12 quite ubiquitously, and some ancient civilizations used to count in base-60. They used a rather innovative trick for counting with their hands. Using their left thumb, they would touch the end segment of the left index finger: that’s 1. Then they would move down to the next segment of that finger: that’s 2. Then the next segment: that’s 3. Now move up to the next finger, top segment: that’s 4. Repeat through all fingers of the left hand. When you get to the last segment on the pinky, count “1″ on the right hand (which means you’ve counted “12″ units total). Five fingers on the right hand means 5 groups of 12, which makes 60 altogether. So “one hand” is “60 units”.

This post is a crash course in non-decimal bases and how to use them. It’s intended for the curious, not necessarily the math majors, so you should be able to wade through with a high-school level math education.

A little background math primer

The following math functions will be referenced throughout this post.

log (base) x

[usage: log (base) x] In general math, the log (short for “logarithm”, implying exponential progression) is usually implied to be a base 10 log (eg. log 100 = 2). For some of the things we’ll be doing on this site, we will need to use other bases though. The answer to a log (base) x expression is the power that (base) must be raised to in order to equal x. In the example earlier, we said log (base10) 100 = 2. 10 raised to the 2nd power = 100, so our answer is 2. Get it?
examples: log (base2) 16 = 4, log (base16) 16 = 1, log (base4) 24 = ~2.294

modulous

[usage: x modulous y] In some groups, the modulous operator is the “%” symbol, to be used like this: x % y. The “modulous” is the remainder left over when you divide x by y. A modulous of zero indicates that x is evenly divisible by y, (meaning it is a multiple of y, and y is a factor of x). It is interesting to note that the modulous will never be larger than the base-system used. That is, if you are using a base-10 system, you will never have a modulous larger than 9.
examples: 10 % 2 = 0, 10 % 3 = 1, 10 % 4 = 2, 16 % 10 = 6

floor( )

[usage: floor (x) ] The floor function truncates (read: knocks off) any decimal, leaving the whole number as is. In layman’s terms it “rounds down” any number to the nearest whole number. Its complement is the ceiling( ) function.
examples: floor(2.1) = 2, floor(2.9999) = 2, floor(1.99999) = 1

ceiling( )

[usage: ceiling (x) ] The ceiling function truncates (read: knocks off) any decimal, raising the whole number to the next largest whole number. It “rounds up” to the next highest whole number. Its complement is the floor( ) function.
examples: ceiling(1.1) = 2, ceiling(1.000001) = 2, ceiling(1.99999) = 2

What is a Base?

That bit of obviousness aside, what is a base? When we say that we are counting in base x, we are utilizing a different number system. The easiest way to explain this is through example, observe:

Base
Lowest
Highest and beyond
2
1
10
11
100
101
110
111
1000
1001
1010
1011
8
1
2
3
4
5
6
7
10
11
12
13
10
1
2
3
4
5
6
7
8
9
10
11
16
1
2
3
4
5
6
7
8
9
A
B

The Base-10 (also called “Decimal”) number system is what nearly every normal person uses on a day to day basis. When we count in base-10, we reach 9, then start back again at 0, but we increase the next place to the left by 1 (if you were adding you would call that “carrying a 1″). The same goes for Base-8 (also called “Octal”). We count 1, 2, 3, 4, 5, 6, 7, then we go back down to 0 and carry-over the 1. Understand? “10″ isn’t really “ten” in octal, it’s 8 units, but it’s written “10″. This definitely requires you to re-think what you know about counting. :)

Base SIXTEEN?

Now if you are looking at the Base-16 (also called “hexadecimal”) line with some puzzlement, I don’t blame you. Why are we using letters for “ten” and “eleven”? You could technically use any kind of non-numeric symbol you wanted, but it’s generally accepted to use A-F for hexadecimal. A sample number sequence in hexadecimal would look like this:

1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
10
11
12
13
14…

It’s very important to think of this in terms of “units” rather than actual numbers. While the numerals 1-9 are equivalent across all the different bases (although some bases, such as binary, don’t use anything higher than 1!), when you reach “10″ and beyond, that’s where it gets tricky. Let’s have another look at binary, as I think it’s the easiest base to get you in the right frame of mind:

0
1
10
11
100
101
110
111
1000
1001
1010
1011
1100
1101
1110
1111
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

I placed a row of regular decimal numbers with the corresponding binary digits for comparison. Do you see the pattern? Binary is base-2. Every time you have two units, you carry over the one. Let’s try a larger table to explain this from a slightly different approach, we’ll use Base-4, counted by column from left to right this time:

0
10
20
30
100
110
120
130
200
210
220
230
300
310
320
330
1
11
21
31
101
111
121
131
201
211
221
231
301
311
321
331
2
12
22
32
102
112
122
132
202
212
222
232
302
312
322
332
3
13
23
33
103
113
123
133
203
213
223
233
303
313
323
333

Read this table top-to-bottom, left-to-right.(start at the top left, go to the bottom of that column, then start at the next column over). Do you see how the progression happens? Do you know what the next number after 333 would be? (Hint: what comes after 999)

For one final approach to explaining this, let’s not even use numerals, let’s make up a series of symbols.We’ll use a base-4 system, as above, but with a symbol system.

@
&@
$@
#@
&@@
&&@
&$@
&#@
$@@
$&@
$$@
$#@
#@@
#&@
#$@
##@
&
&&
$&
#&
&@&
&&&
&$&
&#&
$@&
$&&
$$&
$#&
#@&
#&&
#$&
##&
$
&$
$$
#$
&@$
&&$
&$$
&#$
$@$
$&$
$$$
$#$
#@$
#&$
#$$
##$
#
&#
$#
##
&@#
&&#
&$#
&##
$@#
$&#
$$#
$##
#@#
#&#
#$#
###

Does that make any more sense? The symbols used is really unimportant. We are raised on a decimal system, and we directly associate our numerals with counting-units in the same way we directly associate our alphabet with phonetic sounds.

Converting One Base to Another

So how do you convert a number from one base (i.e. decimal) to another base (i.e. octal)?

If you have ever done any HTML coding before (eg. writing a webpage) you’ll note that colors are always written as hexadecimal trios: #XXYYZZ, each pair standing for Red, Green, or Blue, respectively. Their numbers range from 00 to FF. If you happened to know the decimal values (eg. 127, 96, 127) you would need to convert it to hexadecimal in order to write the pairings properly. That’s a very small example, yes, but it’s the kind of thing we’re dealing with.
Generally speaking though, you won’t need to know how to convert bases for every day use, but then again, most of the stuff on this page is meant for people who like to learn stuff just for the challenge. :)

Ok, so let’s start with something easy, we’ll convert a decimal number into binary. (believe it or not, that is easy). We’ll convert the number 14 (base-10) into its binary (base-2) equivalent. I’m going to do this exhaustively so you can really see every aspect of it, and get a better understanding. Although if you were to do it for real you can cut out of a few of the steps.

Step 1: How Many Digits?

We’re going to establish two variables here: let x = the beginning number in base 10 (the number 14, for this example), and let y = the base to convert to (2, for binary, in this example). To determine how many digits, or places, our answer will be, perform: log (base y) x. In this example, the equation would be the log (base 2) 14. So if you remember from the other page about obscure math functions, the answer is the exponent to which the base must be raised to equal “14″ in this case. Now if you realize that 14 is not a power of 2, you’re correct. We’re actually going to take the ceiling(log (base y) x). This means that we’re going to round up our answer to the next whole number.

exponent
1
2
3
3.8
4
5
answer
2
4
8
13.9288...
16
32

The answer to the log function is approximately 3.8… but since we’re only concerned with the ceiling( ) of it, what you can do is just look for the first number that is too large, and take that corresponding exponent. In this case, 16 is the first number larger than our starting number, so the number we want is “4″. Our answer is 4 digits long

Step 2: How do we convert a digit?

You’ll need to use two functions to convert a digit. Simple division (dropping any remainder), and the Modulous operator (see above, in the beginning)

We’ll start at the right side (the smallest digit) and work our way to the left (the largest digit).

a) Starting number
14
14
14
14
b) Divided by
div
div
div
div
c) (base ^ place)
(2^3)
(2^2)
(2^1)
(2^0)
d) Equals (don’t forget to drop the remainder)
1
3
7
14
e) That number modulated by the base
(1 % 2)
(3 % 2)
(7 % 2)
(14 % 2)
f) Equals
1
1
1
0

Line (a) is our beginning number, the one we wish to convert to base 2 in this instance. If you’re finding line (c) confusing, then maybe you could think about it this way:

  • For the rightmost digit, Let X equal: (Starting-number / base-to-convert to). Then find the rightmost converted digit, it is (X % base-to-convert-to)
  • To find the next-to-the-left digit, Let Y equal: (X / base-to-convert-to), and the converted second digit is (Y % base-to-convert-to)
  • To find the third digit, Let Z equal: (Y / base-to-convert-to), and the converted third digit is (Z % base-to-convert-to)
  • Repeat ad nauseum.

Get it? If you’ve ever worked with scientific notation, then this table (in decimal) should look familiar:

10^4
10^3
10^2
10^1
10^0
10,000
1,000
100
10
1

Do you see the similarity? Starting at the right, each place increases the exponent of the base by 1.  So the first digit is every number from 1 all the way to the base^1 – 1 (because when it reaches the base, you carry it over, right? 9 + 1 = 10 in base ten) The second digit would be everything from the base^1 all the way to base^2 – 1.The third digit is..you guessed it, from base^2 up to base^3 – 1. Seeing a pattern here?