Binary, Hexadecimal, and Other Base NumbersNew
Learning and understanding a different base-numbering system has one very large similarity to learning a new language. You need to truly understand the language/base system you currently use. Before we jump into binary, hexadecimal, and other base numbers, let’s understand how our base-10 numbering system, or decimal, works.
We have a base-10 numbering system because we have 10 fingers. That’s the general belief anyway, and I don’t see a reason to disagree. That makes it easy to flash all of our fingers twice to get 20, three times to get 30, etc. Now, we say “thirty” but seeing it written as “30” helps this make more sense. You break that into two parts and it’s literally “3 sets of 10”.
Mathematically - … 100000*0 + 10000*0 + 1000*0 + 100*0 + 10*3 + 1*0 = 30.
If we do a bigger example, like 582,173, it looks like this.
In essence, 500,000 + 80,000 + 2,000 + 100 + 70 + 3 = 582,173
And to make it a little bit more generic, we can look at those multiples of 10 a different way.
Do you see why it’s called base 10? The base of the exponent is 10! And with each spot you move to the left, the exponent increases by one. It’s important to note that the first spot is raised to the power of 0, which always makes the multiplier 1. Also, while this table only shows the exponent going up to 5, there is no limit to the size of the exponent, but it will always increment by one.
Just in case you have any questions about why 10^0 = 1, let me stop you right now. I have no idea. We’re electrical engineers, not mathematicians. But. What’s important is that anything raised to the 0 is 1. Anything. Except infinity. Infinity… always complicating things.
And something that may not seem important now but will in a little bit, is that with any base system, you need to represent the base numbers in that system with ONE character. With base 10, we have 10 characters to represent every number, 0-9. And don’t forget, 0 is a number, which is how you get 10 characters with 0-9.
Now, let’s move on to binary.
While we have 10 fingers, computers do not. Not yet, anyway. They can only think in terms of “yes” and “no” or “on” and “off”. Therefore, their counting system, their entire language, is based on either 1 (yes - represented as a high voltage) or 0 (no - represented as a low voltage). With that, they have to count in a way that works with this limitation.
With base 10, we had the base number as 10. With binary, we only have 2 options, so we use base 2. It looks like this:
And its equivalent values in decimal are:
Let’s use the number 53, to show how this would be represented in binary.
This yields 32+16+4+1 = 53. I want to note that since this is a 0-1 option, you can only have zero sets or 1 set of each multiplier with binary, but this isn’t the case with other numbering systems.
Remember how in decimal, we have 10 characters to represent 10 values from 0-9? In binary, we have 2 characters to represent 2 values from 0-1. In binary, you will never see the symbols 2-9 that you’ve grown accustomed to in the decimal system.
And this is literally all there is! That doesn’t necessarily mean it’s easy, though, so let’s run through another binary example in a little more depth before moving on.
How to convert from Decimal to Binary?
Let’s use 37 as our next example. First, find the first binary multiplier that is smaller than the number you want to multiply. In this case, it’s 32.
Now, 37-32 leaves us with 5, 37-32=5. As we move to the increasingly smaller numbers, we compare, finding the next binary base that’s smaller than the number we’re looking for. We can see obviously that 16 and 8 are bigger than 5, so let’s put “0” in for those. But 4 is smaller than 5, so we have 1 set of 4.
With 5 minus 4 equalling 1, things get pretty simple by this point. We know that 2 is bigger than one and 1 is equal to one, so we put in a zero and a 1 at the end.
32+4+1 = 37! And, because I make mistakes, I usually run over to our Binary/Hex/Decimal Converter tool and verify that I did it correctly. Another bonus of using that tool is that you can come up with any number you want and practice with it while also making sure you get the right answer.
Going the other way, from binary to decimal, is actually easier, if you have something like this table ready. Let’s use 101110 as an example.
As we’ve already filled it out, we can see that it’s 32+8+4+2 = 46!
So, what is hexadecimal and why is it a thing?
Hexadecimal is base-16, (hex = 6, dec = 10 so hexadec = 16, I guess). So, where as binary has “2” as a base and decimal has “10” as a base, hexadecimal has “16” as a base. Before we go into the complexities of representing a number bigger than 9 with a single character, I just want to emphasize that this works exactly the same as binary and decimal, just with a different base number.
Okay, this is where it can get hard to wrap your head around it, as we can easily think of numbers less than 9 but struggle to think of numbers greater than 9 without using 2 digits. Remember from above where we discussed that the base-10 system uses 10 characters, 0-9, and the base-2 system uses 2 characters, 0-1? Well, base-16 uses 16 characters, 0-F. Look at the table below and you will probably be confused, so just give it a glance before moving on. You can always go back up and look at it again. This table compares counting from 0 to 15 in hexadecimal and decimal.
Counting from 0-15 in Hexadecimal and Decimal
… + 101*0 + 100*1 = 1
… + 101*0 + 100*2 = 2
… + 101*0 + 100*3 = 3
… + 101*0 + 100*9 = 9 (note the transition from 9-10 in base-10)
… + 101*1 + 100*0 = 10
… + 101*1 + 100*1 = 11
… + 101*1 + 100*2 = 12
… + 101*1 + 100*3 = 13
… + 101*1 + 100*4 = 14
… + 101*1 + 100*5 = 15
See what happens after 9? In decimal, it’s “10*1 + 1*0 = 10” - which only makes sense in base-10. In hexadecimal, this doesn’t make sense because we’re using base-16, not base-10, so we need an additional 6 characters. So we use letters instead! A = 10, B = 11, C = 12, D = 13, E = 14, and F = 15. If you notice on our Binary/Hex/Decimal Converter, we only offer up to Base-36, because we ran out of alphabet letters to use and have no idea why you would need to go up that high anyway (If you do, reach out to us, we’d be interested to hear what shenanigans you’re up to!)
Please note that the use of letters is mathematically arbitrary. If we had decided that 10 would be represented by “$” instead, then we would use “$”. But, with English anyway, the alphabet gives a good selection of already familiar and serialized symbols that most everyone both recognizes and already knows their order.
Let’s use 2EA as an example and convert it from hexadecimal to decimal - use the table above as a reference for the values of the hexadecimal numbers:
In this case, it would be 2*256 + 14*16 + 10*1 = 746
Or, another way to look at it is
2*(16^2) + 14*(16^1) + 10*(16^0) = 746
For our base-10 brain, this is hard to keep straight and may seem pointless. Who would use base-16? Actually, we would! Because it’s easier than binary and it’s *very* easy to convert between binary and hexadecimal.
The reason hexadecimal is so easy is that each hexadecimal unit can be represented by four binary units (bits) or vice versa. In other words, hexadecimal and binary are extremely easy to convert. Look at the table below.
As you can see, you can perfectly represent 0 - 15 in hexadecimal with four bits (remember that a bit is a “binary unit”). Now, computers speak in binary and it isn’t a big deal. But when you’re looking at a random number, is it easier to read and remember this?
And with a direct comparison:
If you said you’d prefer the binary, then awesome, you’re a robot. For humans, the second one is easier. And since you can represent one hexadecimal number with exactly four binary numbers, it scales beautifully.
While this only discussed decimal, binary, and hexadecimal specifically, the basic concepts work when dealing with any base-N number, from 2 to infinity. Our recommendation is to work through a couple of conversions with different base numbers to give these concepts some time to sink in and become second nature for you.
A couple key points to remember:
- The base number is the multiplier.
- The exponent of the base number always starts at 0 and always increments by one for each step.
- Any number raised to the “0” is 1. In other words, any number with an exponent of “0” is 1.
- Counting starts at 0, not 1. Which is by base-10 is from 0-9, not 1-10.
- Not understanding this the first time through is okay! But try and practice making some conversions and then come back and read through this again as necessary.
- Finally, always double-check your work with a converter, because mistakes happen.
What about fractions? Or negative numbers? Or really, really big numbers? Or even letters? Yes, these are issues and we are working on some tutorials now to cover how to deal with these in binary. As it is, you’ve got whole numbers down and you should be proud of yourself. We’ll update this page once we get those other tutorials done so you can learn more about how computers deal with numbers and letters.
Can't I just use a converter, why do I need to know this?
Doing these conversions by hand isn't practical, but it's important to have a fundamental intuitive understanding of the different base number systems when working with microcontrollers.
Get the latest tools and tutorials, fresh from the toaster.