# Number Systems: An Introduction to Binary, Hexadecimal, and More

Ever see crazy binary numbers and wonder what they meant? Ever see numbers with letters mixed in and wonder what is going on? You'll find out all of this and more in this article. Hexadecimal doesn't have to be scary.

*(Thanks to the ReBoot Wiki for the thumbnail image.)*

## 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.

## Activity

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 View Guess button. 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 is going on. The right-most digit represents the number of ones, the next digit represents the number of tens, the next the number of hundreds, etc.

## Visualizing Base-10

Confused by these descriptions? No problem - below is a demo to help you out. Simply enter a number in the text box and click draw. Try entering a large number, like 2347. You'll see 2 groups of one thousand, 3 groups of one hundred, 4 groups of ten, and 7 individual blocks.

## Base-10 Mathematically

You may have noticed a pattern by now. Let's look at what is 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`10`

.^{3} - There are 3 groups of a hundred. Again, not coincidentally,
`100 = 10*10`

or`10`

.^{2} - There are 4 groups of ten, and,
`10 = 10`

.^{1} - Finally, there are 7 groups of one, and
`1 = 10`

. (That may seem strange, but^{0}*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*10`

^{2}+9*10^{1}+2*10^{0}`1147 = 1*10`

^{3}+1*10^{2}+4*10^{1}+7*10^{0}`53 = 5*10`

^{1}+3*10^{0}

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.

## Base-8

On to base-8, also called octal. Base-8 means just what is 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 10_{8}.

*(Editor's note: 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 10 _{8}, I read "octal one-oh" or "one-oh in base-eight". For 10_{10} I read "decimal one-oh" or "one-oh in base-ten".)*

Great, so we know 10_{8} represents eight items. (Always feel free to plug a number into the first tool for a visualization.) What's the next number after 77_{8}? If you said 100_{8}, you're correct. We know from what we've learned so far that the first 7 in 77_{8} represents groups of 8, and the second 7 represents induvidual items. If we add these all up, we have `7*8 + 7*1 = 63`

. So we have a total of 63_{10}. So 77_{8}=63_{10}. We all know 64_{10} comes after 63_{10}.

## Converting From Base-8 to Base-10

Let's look at a wordier example now. John offers to give you 47_{8} cookies, and Jane offers to give you 43_{10} cookies. Whose offer do you take? If you want, go ahead and generate the graphic for 47_{8} graphic 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 47_{8} 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 = 39`

. So, 47_{10}_{8} cookies is the exact same as 39_{10} 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 523_{8}. There are five groups of 8^{2}, two groups of 8^{1} and three groups of 8^{0} (remember, 8^{0}=1). If we add these all up, `5*8`

, we get 339^{2} + 2*8^{1} + 3*8^{0} = 5*64+2*8+3 = 339_{10} which is our final answer. The diagram below shows the same thing visually:

Here are a couple more examples:

`111`

_{8}= 1*8^{2}+1*8^{1}+1*8^{0}= 64+8+1 = 73_{10}`43`

_{8}= 4*8^{1}+3*8^{0}= 32+3 = 35_{10}`6123`

_{8}= 6*8^{3}+1*8^{2}+2*8^{1}+3*8^{0}= 3072+64+16+3 = 3155_{10}

## 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: 150_{10}.

We first find the largest power of 8 that is smaller than our number. Here, this is 8^{2} or 64 (8^{3} 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 8^{1} (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 226_{8}.

In fact, we can make this process a touch clearer with math. Here are the steps:

- 150/8
^{2}= 2 remainder 22 - 22/8
^{1}= 2 remainder 6 - 6/8
^{0}= 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 that 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 d_{4}d_{3}d_{2}d_{1}d_{0} 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 `d`

._{4}*b^{4} + d_{3}*b^{3} + d_{2}*b^{2} + d_{1}*b^{1} + d_{0}*b^{0}

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*4`

. We could, or course, follow this pattern with any amount of digits in our number.^{4} + 2*4^{3} + 3*4^{2} + 1*4^{1} + 1*4^{0} = 3*256 + 2*64 + 3*16 + 1*4 + 1*1 = 949

## Base-16

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:

Other than these extra digits, hexadecimal is just like any other base. For example, let's convert 3D_{16} to base-10. Following our previous rules, we have: `3D`

. So 3D_{16} = 3*16^{1} + 13*16^{0} = 48 + 13 = 61_{16} is equal to 61_{10}. Notice how we use D's value of 13 in our calculation.

We can convert from base-10 to base-16 similar to the way we did with base-8. Let's convert 696_{10} to base-16. First, we find the largest power of 16 that is less than 696_{10}. This is 16^{2}, or 296. Then:

- 696/16
^{2}= 2 remainder 184 - 184/16
^{1}= 11 remainder 8 - 8/16
^{1}= 8 remainder 0

We have to replace 11 with its digit representation B, and we get 2B8_{16}.

Feel free to try some more conversions for practice. You can use the application below to check your answers:

## Binary! (Base-2)

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:

Can you figure out what it means?

Let's try a few conversions with base-2. First, we'll convert 101100_{2} to base-10. We have: `101100 = 1*2`

.^{5} + 1*2^{3} + 1*2^{2} = 32 + 8 + 4 = 44_{10}

Now let's convert 65 to binary. 2^{6} is the highest power of 2 less than 65, so:

- 65/2
^{6}= 1 remainder 1 - 1/2
^{5}= 0 remainder 1 - 1/2
^{4}= 0 remainder 1 - 1/2
^{3}= 0 remainder 1 - 1/2
^{2}= 0 remainder 1 - 1/2
^{1}= 0 remainder 1 - 1/2
^{0}= 1 remainder 0

And thus we get our binary number, 1000001.

Understanding binary is super important. I've included a table below to point out digits' values.

For example, the value of 10001 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, its 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 number to base-10
- Convert result to 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, say we have the number 1011101_{2}. 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 was its own separate number, which gives us 5 and 13. Finally, we simply use the corresponding hexadecimal digits to write out base-16 number, 5D_{16}.

We can go the other direction also, by converting each hexadecimal digit into four binary digits. Try converting B7_{16} to binary. You should get 10110111_{2}.

This trick works because 16 is a power of 2. What this means is that we use similar trick for base-8, which is also a power of 2:

Of course, you can reverse the process to go from base-8 to binary also.

## Conclusion

Let's go all the way back and revisit the color guessing game.

In Flash, colors are stored as a single number. 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 17FF18_{16} we can easily tell that our red component is 17_{16} or 23_{10}. Our green component is FF_{16}, or 255_{10}. Finally our blue component is 18_{16} or 24_{10}. If we are given the base-10 version of our color, 1572632_{10}, 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! Feel free to grab the source code from any of the demos. Also, if you have any questions, please ask them below.

Tuts+ tutorials are translated into other languages by our community members—you can be involved too!

Translate this postPowered by