Teaching binary and other bases
By stretch | Thursday, March 4, 2010 at 4:56 a.m. UTC
I've seen a lot of people struggle when first learning decimal-to-binary and other conversions, mostly because they find themselves overwhelmed with conversion charts and don't quite grasp the concept of a numbering base. I decided to write this post when I realized the method I use to teach people binary and hexadecimal isn't used in any of the books I have (which isn't to say it's unique by any means, but perhaps not very widespread).
If you do use the methodology described here, I would appreciate feedback on how well it worked (or didn't) so that it can be improved.
Step 1: Relearn to Count in Decimal
Think back to kindergarten or whenever you first learned to count. As a typical example, let's say you have seventeen apples. To express this as a decimal value, start with zero and begin counting: 1, 2, 3... up to 9. When you reach the tenth apple, you've run out of digits. This is because we count in a base-10 numbering system, more commonly known as decimal, which employs the digits 0 through 9.
To express values greater than the highest available digit (e.g. values greater than 9), we must use multiple digits. We increase the column to the left (which is zero if not present) by one and reset the current column to zero; 09 becomes 10. This is a recursive process; it is repeated as necessary until we reach the leftmost column of the number (e.g. counting from 999 to 1000).
Here's the key concept to absorb: every numbering base works this way. The only difference is the amount of digits you can use.
Step 2: Counting with Fewer Digits
Now let's try counting to seventeen again, but this time you can't use the digits 8 or 9. We begin counting, 1, 2, 3... 7. Since we're out of available digits at this point, we increase the column to the left from 0 (or nothing) to 1, and reset the current column to zero. We continue increasing the value of this column again until we run out of digits a second time. The column to the left is again increased and the current column reset. We continue increasing the value of the rightmost column again until we run out of apples to count.
Congratulations, you now know how to count in octal (or base-8), which uses only the digits 0 through 7.
Step 3: Counting with Even Fewer Digits
Let's extend this concept to its extreme; for this exercise, you can only use the digits 0 and 1. Start counting: 0, 1 -- we're already out of digits, so add another column, resume counting, and repeat. Notice that we have to increase the column to the left and start over for every other apple we count.
And now you can count in binary.
One note here: binary numbers are typically written in groups of eight bits (binary digits), with leading zeros for padding. This is done out of convention simply because this is typically the smallest amount of bits a computer deals with at any one time.
Step 4: Counting with More Digits
"What do you mean, more digits? We only have ten!" True, we only have ten numeric digits; that's why we begin adding letters to the mix for bases like hexadecimal (base-16) or "hex" for short.
Note that hexadecimal numbers are normally denoted with a leading "0x"; the number in the above example would typically be written 0x11. Padded to fill a 32-bit variable, it would be written 0x00000011.
Step 5: Converting Back to Decimal
To convert a number from another base to a decimal number, each digit is multiplied by the value of its place. To illustrate this, first we example a the decimal number - 742, for instance - can be expressed as the sum of its place values:
The value of each place is derived as the base number (10 for decimal, 8 for octal, etc.) to the power of its column (starting with zero). In other words:
(A quick math tip: remember that any number to a power of zero equals one.)
Realizing this, we can easily extend this concept to converting other bases to decimal. Let's try our octal number 21:
Conversion from binary takes a bit longer but works exactly the same way:
It just takes a bit of practice to begin memorizing the first eight powers of two (1, 2, 4, 8, 16, 32, 64, and 128). Once you've got them locked in your head, converting between decimal and binary becomes second nature.
About the Author
Jeremy Stretch is a network engineer living in the Raleigh-Durham, North Carolina area. He is known for his blog and cheat sheets here at Packet Life. You can reach him by email or follow him on Twitter.
Posted in Education
March 4, 2010 at 6:52 a.m. UTC
I have not found this methodology in any of the books till now. It is very effective and simple to learn easy and fast. Keep up the good work.
March 4, 2010 at 7:09 a.m. UTC
I always tell myself number is just another character to represent quantity. so if is Hex, we have more character use. for Binary we only have 2 character to use. But this is my way of understanding how to count in difference bases.
March 4, 2010 at 9:52 a.m. UTC
there is a little mistake on the last calculation.. binary 1001 = 9 and is not 16 since 2^3 = 8 and is not 16.
March 4, 2010 at 1:30 p.m. UTC
Someone has to say this, so let me be the first ... "there's 10 kinds of people in the world, those that understand binary, and those that don't." :D
I enjoy teaching my two twelve year olds about binary and hex. My son gets it, daughter doesn't.
It's all columns representing places, and digits in those columns acting as multipliers. Multiply the places, add them together.
Cool post Stretch.
March 4, 2010 at 2:34 p.m. UTC
Fixed the typos in the images, thanks for the heads up! It's funny how long I can stare at something like this without noticing stuff like that. =\
March 4, 2010 at 4:14 p.m. UTC
Sorry to be picky. Still a typo on the last one.
1 x 2^4 = 16 <--- you have 1 x 2^4 = 1
March 4, 2010 at 4:54 p.m. UTC
@jnftech: Fixed, thanks!
March 5, 2010 at 12:36 p.m. UTC
It also can be useful to add a couple math tips.
- binary shifting to the left means multiply by 2
00000101 means 5
00001010 means 10
00010100 means 20 and so on
- rightmost binary 1 means odd number... always =)
00000001 - 1
00000011 - 3
00000101 - 5
March 5, 2010 at 6:10 p.m. UTC
I just "discovered" the new Calculator in Windows 7
- open up "calc" from the run line
- click on the "view" menu and change it to "programmer"
- by default, you should be in DECimal and the Qword shown below.
Now you quickly and easily see how manipulating decimal numbers equate to places in binary.
And you can play around with HEXadecimal and OCTal.
I think it's pretty cool. :)
March 8, 2010 at 6:04 p.m. UTC
Very good and concise write up.
I like how you are taking the pattern for base 10 and altering it for other bases.
March 12, 2010 at 9:30 p.m. UTC
Brannen - the Windows calculator functions you "discovered" date back to at least XP.
March 23, 2010 at 7:49 a.m. UTC