What is binary? Well, that’s easy. It’s a base-2 number system. We use decimal which is a base-10 number system. Also in common use are hexadecimal (base-16) and octal (base-8).
Let’s consider what happens when we are counting from 1 to 11 in decimal. We start at 0 and go up to 9. Once we reach the number after 9, we reset the ones digit to 0 and increment the tens digit by one. In binary we do the same thing except instead of it happening after 9, it happens after 1. Also, instead of calling it the tens digit, we call it the twos digit, fours digit, eights digit, and on up. One of the easiest ways to convert from binary to decimal and back is to go to the most significant digit and figure out the biggest number you can make using that in the incoming number system. If you look at 1001 in binary, you know that 1000 is 8. So now you have 8 in decimal and 0001 in binary. Well, 0001 in binary is 1 in decimal so you have 9 as your decimal answer. If you want to convert 18 to decimal, you know that there is a 16s spot in decimal. That’s 00010000. That leaves you with 2 in decimal, 00010000 in binary. 2 in binary is 0010 so that gives you 00010010 in binary.
Why do I always do binary in groups of 4 bits? It has to do with computing and hexadecimal notation. Electrical engineers and computer science guys tend to do that by habit. It’s a good habit, in my opinion. Before I move on to hex, I want to talk about Binary Coded Decimal real quick. BCD is how we rapidly represent a decimal number as a binary number for the purposes of putting it on a 7 segment display.
In Binary Coded Decimal, you just represent each decimal digit as a binary number. Instead of 12 in decimal being 00011000, we call it 0001 0010. Note that there’s a space between the first 4 bit segment and the second 4 bit segment. If you ever see a binary clock, it’s probably outputting in BCD. Very few of them output in direct binary which is a little disappointing. Now you can lord over your nerd-friends that their binary clock isn’t true binary. King of the Geeks, indeed. In addition to standard binary and BCD, there’s also one’s compliment, two’s compliment, and gray code binary counting schemes. I’ll talk about those in a another article when we delve into transistor optimization algorithms and how to subtract numbers without subtraction. Woo!
In hexadecimal, we make things more confusing by needing more symbols to represent the number after 9. We use the alphabet up to F. 1A in hexadecimal is 26 in decimal and 11010 in binary.
At this point, you’re probably thinking “who cares? This is for the nerds.” Well, you’re right except you’re the one that clicked on this article. Binary is important because it is the easiest way to pass data via electronic means. To pass data, you can simply pass it as a series of “on” and “off” voltage pulses. If you want to pass the hexadecimal code for 1A (which is an eight bit number that could correspond to a command to the processor) you would pass the binary equivalent (technically, you’d shift the voltage of a carrier wave on each clock pulse so the machine can differentiate between 001 and 011 as they’d look the same otherwise but that’s another topic entirely).
Hexadecimal is used in more computer applications than I could possibly name. The most commonly seen application-layer manifestation is in color coding. #000000 represents black, #FFFFFF represents white, #00008B represents blue, et cetera. Memory addressing also uses hexadecimal code. Ever notice a bunch of numbers and letters garbled together in error messages that your computer gives you? Those are more than likely hexadecimal names of memory addresses. The computer is telling you that something (probably an overflow) horrible has happened at that address. Kind of like a cop calling in a robbery at a specific business, your computer is telling you that something bad has happened and is hoping you can fix it.
Why is hexadecimal so useful? Well, you can swap back and forth between hex and binary very easily. A hexadecimal digit is just a 4 bit binary number.
I made a chart to show how to count from 0 to 16 in all 3 number systems. There are several ways to convert back and forth. Personally, I prefer to go from decimal to binary to hexadecimal. Each 4 bit group of binary coresponds with a hexadecimal digit. For example, 1010 will always be the digit”A.” We can therefore conclude that 10101010 is “AA” without having to think about it. If we know that “F” is “1111” then twenty-four 1s in a row is the hexadecimal for “FFFFFF” which is the color “white” in RGB coloring systems. RGB color systems have a two digit hex code for how much red, green, and blue each color contains. 0000FF is full blue, for example. With our newfound knowledge, we can quickly discover that when making a website, FF00FF will be purple since we know that red and blue make purple. Now you can impress your web developer/photography friends with not needing a hex color code chart to describe colors. If they find that type of thing impressive, of course.
Edit: Thanks to reader Thad for catching a typographical error in the conversion chart.