Ever see crazy binary numbers and wonder what they meant? Ever see numbers with letters mixed in and wonder what's going on? You’ll find out all of this and more in this article. Hexadecimal doesn’t have to be scary.
Introduction: What Is a Number System?
You probably already know what a number system is—ever hear of binary numbers or hexadecimal numbers? Simply put, a number system is a way to represent numbers. We are used to using the base-10 number system, which is also called decimal. Other common number systems include base-16 (hexadecimal), base-8 (octal), and base-2 (binary).
In this article, I’ll explain what these different systems are, how to work with them, and why knowing about them will help you.
Before we get started, let’s try a little activity for fun. There are many different ways to represent a color, but one of the most common is the RGB color model. Using this model, every color is made up of a combination of different amounts of red, green, and blue.
You may be wondering how colors relate to number systems. In short, on a computer, any color is stored as a large number: a combination of red, green, and blue. (We’ll go into more detail on this later.) Because it’s just a number, it can be represented in multiple ways using different number systems.
Your job is to guess how much red, green, and blue is in the background color of the activity below. The values for red, green, and blue can range from 0 to 255.
Feel free to use the various hints provided to help you out. If you don’t understand the numerical hints yet, no problem! You can see what your guess looks like using the Check Guess button. And if the background color happens to make the text difficult to read, hit New Color. Right now, it may seem tricky, but hopefully by the end of the article, it will seem easy.
Looking at Base-10
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11... You’ve counted in base-10 all of your life. Quick, what is 7+5? If you answered 12, you are thinking in base-10. Let’s take a closer look at what you’ve been doing all these years without ever thinking about it.
Let’s take a quick look at counting. First, you go through all the digits: 0, 1, 2... Once you hit 9, you have no more digits to represent the next number. So you change it back to 0 and add 1 to the tens digit, giving you 10. The process repeats over and over, and eventually you get to 99, where you can’t make any larger numbers with two digits, so you add another, giving you 100.
Although that’s all very basic, you shouldn’t overlook what's going on. The rightmost digit represents the number of ones, the next digit represents the number of tens, the next the number of hundreds, etc.
Confused by these descriptions? No problem—let’s visualize it instead. Imagine a large number, like 2347. We can represent that with two groups of one thousand, three groups of one hundred, four groups of ten, and seven individual blocks.
Use the tool below to output a number into its composite “groups”.
You may have noticed a pattern by now. Let’s look at what's going on mathematically, using 2347 as an example.
- As you saw, there are 2 groups of a thousand. Not coincidentally,
1000 = 10*10*10, which can also be written as
- There are 3 groups of a hundred. Again, not coincidentally,
100 = 10*10or
- There are 4 groups of ten, and,
10 = 101.
- Finally, there are 7 groups of one, and
1 = 100. (That may seem strange, but any number to the power of 0 equals 1, by definition.)
This is essentially the definition of base-10. To get a value of a number in base-10, we simply follow that pattern. Here are a few more examples:
892 = 8*102+9*101+2*100
1147 = 1*103+1*102+4*101+7*100
53 = 5*101+3*100
Admittedly, this all seems a little silly. We all know what value a base-10 number is because we always use base-10, and it comes naturally to us. As we’ll see soon, though, if we understand the patterns in the background of base-10, we can understand other bases better.
On to base-8, also called octal. Base-8 means just what it sounds like: the system is based on the number eight (as opposed to ten). Remember how in base-10 we had ten digits? Now, in base-8, we are limited to only eight digits: 0, 1, 2, 3, 4, 5, 6, and 7. There’s no such thing as 8 or 9.
We count the same way as we normally would, except with only eight digits. Instead of a lengthy explanation, simply try out the demo below by clicking Count Up 1 to see how counting in base-8 works.
You should notice a similar pattern to before; after we get to 7, we run out of different digits for any higher number. We need a way to represent eight of something. So we add another digit, change the 7 back to 0, and end up with 10. Our answer of 10 in base-8 now represents what we would normally think of as 8 in base-10.
Talking about numbers written in multiple bases can be confusing. For example, as we have just seen, 10 in base-8 is not the same as 10 in base-10. So, from this point on, I’ll use a standard notation where a subscript denotes the base of numbers if needed. For example, our base-8 version of 10 now looks like 108.
I find it a lot easier to understand this if I change the way I read these numbers in my head, too. For example, for 108, I read “octal one-oh” or “one-oh in base-eight”. For 1010 I read “decimal one-oh” or “one-oh in base-ten”.
Great, so we know 108 represents eight items. (Always feel free to plug a number into the first tool for a visualization.) What’s the next number after 778? If you said 1008, you’re correct. We know from what we’ve learned so far that the first 7 in 778 represents groups of 8, and the second 7 represents individual items. If we add these all up, we have
7*8 + 7*1 = 63. So we have a total of 6310. So 778=6310. We all know 6410 comes after 6310.
Converting From Base-8 to Base-10
Let’s look at a wordier example now. John offers to give you 478 cookies, and Jane offers to give you 4310 cookies. Whose offer do you take? If you want, go ahead and generate the graphic for 478 with the first tool. Let’s figure out its base-10 value so we can make the best decision!
As we saw when counting, the four in 478 represents the number of groups of eight. This makes sense—we are in base-8. So, in total, we have four groups of eight and seven groups of one. If we add these all up, we get
4*8 + 7*1 = 3910. So 478 cookies is the same as 3910 cookies. Jane’s offer seems like the best one now!
The pattern we saw before with base-10 holds true here also. We’ll look at 5238. There are five groups of 82, two groups of 81, and three groups of 80 (remember, 80=1). If we add these all up,
5*82 + 2*81 + 3*80 = 5*64+2*8+3 = 339, we get 33910 which is our final answer. The diagram below shows the same thing visually:
Here are a couple more examples:
1118 = 1*82+1*81+1*80 = 64+8+1 = 7310
438 = 4*81+3*80 = 32+3 = 3510
61238 = 6*83+1*82+2*81+3*80 = 3072+64+16+3 = 315510
Converting From Base-10 to Base-8
Converting from base-10 to base-8 is a little trickier, but still straightforward. We basically have to reverse the process from above. Let’s start with an example: 15010.
We first find the largest power of 8 that is smaller than our number. Here, this is 82 or 64 (83 is 512). We count how many groups of 64 we can take from 150. This is 2, so the first digit in our base-8 number is 2. We have now accounted for 128 out of 150, so we have 22 left over.
The largest power of 8 that is smaller than 22 is 81 (that is, 8). How many groups of 8 can we take from 22? Two groups again, and thus our second digit is 2.
Finally, we are left with 6, and can obviously take 6 groups of one from this, our final digit. We end up with 2268.
In fact, we can make this process a touch clearer with math. Here are the steps:
- 150/82 = 2 remainder 22
- 22/81 = 2 remainder 6
- 6/80 = 6
Our final answer is then all of our non-remainder digits, or 226. Notice that we still start by dividing by the highest power of 8 that is less than our number.
Dealing With Any Base
It’s important to be able to apply the concepts we’ve learned about base-8 and base-10 to any base. Just as base-8 had eight digits and base-10 had ten digits, any base has the same number of digits as its base. So base-5 has five digits (0-4), base-7 has seven digits (0-6), etc.
Now let’s see how to find the base-10 value of any number in any base. Say we are working in base-b, where b can be any positive integer. We have a number d4d3d2d1d0 where each d is a digit in a number. (The subscripts here don’t refer to the base of the number but simply differentiate each digit.) Our base-10 value is simply
d4*b4 + d3*b3 + d2*b2 + d1*b1 + d0*b0.
Here’s an example: we have the number 32311 in base-4. Notice how our number only has digits from zero to three since base-4 only has four total digits. Our base-10 value is
3*44 + 2*43 + 3*42 + 1*41 + 1*40 = 3*256 + 2*64 + 3*16 + 1*4 + 1*1 = 949. We could, of course, follow this pattern with any amount of digits in our number.
Base-16 is also called hexadecimal. It’s commonly used in computer programming, so it’s very important to understand. Let’s start with counting in hexadecimal to make sure we can apply what we’ve learned about other bases so far.
Since we are working with base-16, we have 16 digits. So we have 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... and yikes! We’ve run out of digits, but we still need six more. Perhaps we could use something like a circled 10?
The truth is, we could, but this would be a pain to type. Instead, we simply use letters of the alphabet, starting with A and continuing to F. Here’s a table with all the digits of base-16:
|Base 16 Digit||Value|
Other than these extra digits, hexadecimal is just like any other base. For example, let's convert 3D16 to base-10. Following our previous rules, we have:
3D16 = 3*161 + 13*160 = 48 + 13 = 61. So 3D16 is equal to 6110. Notice how we use D's value of 13 in our calculation.
We can convert from base-10 to base-16 in a similar way to what we did with base-8. Let's convert 69610 to base-16. First, we find the largest power of 16 that is less than 69610. This is 162, or 256. Then:
- 696/162 = 2 remainder 184
- 184/161 = 11 remainder 8
- 8/160 = 8 remainder 0
We have to replace 11 with its digit representation B, and we get 2B816.
Feel free to try some more conversions for practice. You can use the application below to check your answers:
On to the famous base-2, also called binary. While everyone knows binary is made up of 0s and 1s, it is important to understand that it is no different mathematically than any other base. There’s an old joke that goes like this:
“There are only 10 types of people in the world: those who understand binary and those who don’t.”
Can you figure out what it means?
Let’s try a few conversions with base-2. First, we’ll convert 1011002 to base-10. We have:
101100 = 1*25 + 1*23 + 1*22 = 32 + 8 + 4 = 4410.
Now let’s convert 65 to binary. 26 is the highest power of 2 less than 65, so:
- 65/26 = 1 remainder 1
- 1/25 = 0 remainder 1
- 1/24 = 0 remainder 1
- 1/23 = 0 remainder 1
- 1/22 = 0 remainder 1
- 1/21 = 0 remainder 1
- 1/20 = 1 remainder 0
And thus we get our binary number, 10000012.
Understanding binary is super important. I’ve included a table below to point out digits’ values.
For example, the value of 100012 is 17, which is the sum of the values of the two 1 digits (16+1). This is nothing different than we have done before—it's just presented in an easy-to-read way.
Tricks and Tips
Normally, when converting between two bases that aren’t base-10, you would do something like this:
- Convert the number to base-10
- Convert the result to the desired base
However, there’s a trick that will let you convert between binary and hexadecimal quickly. First, take any binary number and divide its digits into groups of four. So let's say we have the number 10111012. Divided up, we have 0101 1101. Notice how we can just add extra zeroes to the front of the first group to make even groups of 4. We now find the value for each group as if it were its own separate number, which gives us 5 and 13. Finally, we simply use the corresponding hexadecimal digits to write out the base-16 number, 5D16.
We can also go in the other direction, by converting each hexadecimal digit into four binary digits. Try converting B716 to binary. You should get 101101112.
This trick works because 16 is a power of 2. What this means is that we use a similar trick for base-8, which is also a power of 2:
Of course, you can also reverse the process to go from base-8 to binary.
Here are a few other examples:
Converting Binary to Hexadecimal
Let's try to convert 1001111001102 to hexadecimal. When we break it into units of 4, we get 1001, 1110, and 0110. Their values in decimal are 9, 14, and 6 respectively. Converting them to hexadecimal will give us 9, E, and 6. So our number will be 9E6 in hexadecimal.
Converting Hexadecimal to Binary
Now we will change the hexadecimal number A1D16 to binary. We begin by converting each separate digit to its decimal counterpart. A, 1, and D therefore become 10, 1, and 13 respectively. Now we just convert these decimals into binary to get 1010, 0001, and 1101 respectively. The final binary representation becomes 1010000111012.
Converting Binary to Octal
We can use a similar technique to convert binary to octal. This time, we will divide our numbers into groups of 3 because 2^3 = 8. Let's say you want to convert 11111012 to its octal value. Dividing it into units of 3 starting from the rightmost end will give us 1, 111, and 101. Now, we can just calculate the value of these segments to get the octal representation. None of the units will have a value above 7, so we can just combine them directly to get the octal value. The values 1, 111, and 101 will become 1, 7, and 5 in octal respectively. So our number in octal representation will be 1758.
Converting Octal to Binary
Finally, we will convert the octal number 72458 to binary. Separating the number into individual digits will give us 7, 2, 4, and 5. We can now convert them to binary such that each representation has 3 digits. This means that 7, 2, 4, and 5 will become 111, 010, 100, and 101 respectively. Combining them all will give us 111010100101 as the correct binary representation.
Let’s go all the way back and revisit the color guessing game.
When converted to hexadecimal, the first two digits represent the amount of red, the next two the amount of green, and the final two the amount of blue. So, if our color is 17FF1816, we can easily tell that our red component is 1716, or 2310. Our green component is FF16, or 25510. Finally, our blue component is 1816, or 2410. If we are given the base-10 version of our color, 157263210, we need to convert it to hexadecimal before we can tell anything about it.
Try the game again, and see how much better you can do!
Understanding different number systems is extremely useful in many computer-related fields. Binary and hexadecimal are very common, and I encourage you to become very familiar with them. Thanks for reading—I hope you’ve learned a lot from this article! If you have any questions, please ask them below.