tag:code.tutsplus.com,2005:/categories/coding-conceptsEnvato Tuts+ Code - Coding Concepts2012-02-13T07:00:21Ztag:code.tutsplus.com,2005:PostPresenter/active-10848Number Systems: An Introduction to Binary, Hexadecimal, and More<p>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.</p>
<hr>
<h2>Introduction: What is a Number System?</h2>
<p>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).</p>
<p>In this article, I’ll explain what these different systems are, how to work with them, and why knowing about them will help you.</p>
<hr>
<h2>Activity</h2>
<p>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.</p>
<p>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.</p>
<p> 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.</p>
<p> 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 <strong>Check Guess</strong> button. And if the background color happens to make the text difficult to read, hit <strong>New Color</strong>. Right now, it may seem tricky, but hopefully by the end of the article, it will seem easy.</p>
<iframe src="https://codepen.io/tutsplus/embed/Qqmozz/?height=600&theme-id=12451&default-tab=result&embed-version=2" width="850" height="600" frameborder="no" allowfullscreen="true" scrolling="no"></iframe>
<h2>Looking at Base-10</h2>
<p>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. </p>
<p>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.</p>
<p>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.</p>
<hr>
<h2>Visualizing Base-10</h2>
<p>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.</p><figure class="post_image"><img alt="Visualizing Base-10" data-src="https://cms-assets.tutsplus.com/uploads/users/30/posts/9306/image/10.png"><figcaption>Visualizing Base-10 using blocks</figcaption></figure>
<hr>
<p>Use the tool below to output a number into its composite “groups”.</p>
<iframe src="https://codepen.io/tutsplus/embed/aLYxME/?height=400&theme-id=12451&default-tab=result&embed-version=2" width="850" height="400" frameborder="no" allowfullscreen="true" scrolling="no"></iframe>
<h2>Base-10 Mathematically</h2>
<p>You may have noticed a pattern by now. Let’s look at what is going on mathematically, using 2347 as an example.</p>
<ul>
<li>As you saw, there are 2 groups of a thousand. Not coincidentally, <code>1000 = 10*10*10 </code> which can also be written as <code>10<sup>3</sup></code>.</li>
<li>There are 3 groups of a hundred. Again, not coincidentally, <code>100 = 10*10</code> or <code>10<sup>2</sup></code>.</li>
<li>There are 4 groups of ten, and, <code>10 = 10<sup>1</sup></code>.</li>
<li>Finally, there are 7 groups of one, and <code>1 = 10<sup>0</sup></code>. (That may seem strange, but <em>any</em> number to the power of 0 equals 1, by definition.)</li>
</ul>
<p>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:</p>
<ul>
<li><code>892 = 8*10<sup>2</sup>+9*10<sup>1</sup>+2*10<sup>0</sup></code></li>
<li><code>1147 = 1*10<sup>3</sup>+1*10<sup>2</sup>+4*10<sup>1</sup>+7*10<sup>0</sup></code></li>
<li><code>53 = 5*10<sup>1</sup>+3*10<sup>0</sup></code></li>
</ul>
<p>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.</p>
<hr>
<h2>Base-8</h2>
<p>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.</p>
<p> 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 <strong>Count Up 1</strong> to see how counting in base-8 works.</p>
<iframe src="https://codepen.io/tutsplus/embed/xXWBOK/?height=400&theme-id=12451&default-tab=result&embed-version=2" width="850" height="400" frameborder="no" allowfullscreen="true" scrolling="no"></iframe>
<p>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.</p>
<p>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.</p>
<p><em>(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 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”.)</em></p>
<p>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 induvidual items. If we add these all up, we have <code>7*8 + 7*1 = 63</code>. So we have a total of 6310. So 778=6310. We all know 6410 comes after 6310.</p>
<hr>
<h2>Converting From Base-8 to Base-10</h2>
<p>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 graphic with the first tool. Let’s figure out its base-10 value so we can make the best decision!</p>
<p>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 <code>4*8 + 7*1 = 3910</code>. So, 478 cookies is the exact same as 3910 cookies. Jane’s offer seems like the best one now!</p>
<p>The pattern we saw before with base-10 holds true here also. We’ll look at 5238. There are five groups of 8<sup>2</sup>, two groups of 8<sup>1</sup> and three groups of 8<sup>0</sup> (remember, 8<sup>0</sup>=1). If we add these all up, <code>5*8<sup>2</sup> + 2*8<sup>1</sup> + 3*8<sup>0</sup> = 5*64+2*8+3 = 339</code>, we get 33910 which is our final answer. The diagram below shows the same thing visually:</p><figure class="post_image"><img alt="Converting 523 from base-8 to base-10" data-src="https://cms-assets.tutsplus.com/uploads/users/30/posts/9306/image/base-8-to-10.png"><figcaption>Converting 523 from base-8 to base-10</figcaption></figure>
<p>Here are a couple more examples:</p>
<ul>
<li><code>1118 = 1*8<sup>2</sup>+1*8<sup>1</sup>+1*8<sup>0</sup> = 64+8+1 = 7310</code></li>
<li><code>438 = 4*8<sup>1</sup>+3*8<sup>0</sup> = 32+3 = 3510</code></li>
<li><code>61238 = 6*8<sup>3</sup>+1*8<sup>2</sup>+2*8<sup>1</sup>+3*8<sup>0</sup> = 3072+64+16+3 = 315510</code></li>
</ul>
<hr>
<h2>Converting from Base-10 to Base-8</h2>
<p>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.</p>
<p>We first find the largest power of 8 that is smaller than our number. Here, this is 8<sup>2</sup> or 64 (8<sup>3</sup> 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.</p>
<p>The largest power of 8 that is smaller than 22 is 8<sup>1</sup> (that is, 8). How many groups of 8 can we take from 22? Two groups again, and thus our second digit is 2.</p>
<p>Finally, we are left with 6, and can obviously take 6 groups of one from this, our final digit. We end up with 2268.</p>
<p>In fact, we can make this process a touch clearer with math. Here are the steps:</p>
<ol>
<li> 150/8<sup>2</sup> = 2 remainder 22 </li>
<li> 22/8<sup>1</sup> = 2 remainder 6</li>
<li> 6/8<sup>0</sup> = 6</li>
</ol>
<p> 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.</p>
<hr>
<h2>Dealing With Any Base</h2>
<p>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.</p>
<p>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 <em>b</em> 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 <code>d4*b<sup>4</sup> + d3*b<sup>3</sup> + d2*b<sup>2</sup> + d1*b<sup>1</sup> + d0*b<sup>0</sup></code>.</p>
<p>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 <code>3*4<sup>4</sup> + 2*4<sup>3</sup> + 3*4<sup>2</sup> + 1*4<sup>1</sup> + 1*4<sup>0</sup> = 3*256 + 2*64 + 3*16 + 1*4 + 1*1 = 949</code>. We could, or course, follow this pattern with any amount of digits in our number.</p>
<hr>
<h2>Base-16</h2>
<p>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.</p>
<p>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?</p>
<p>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:</p><table>
<thead><tr>
<th>Base 16 Digit</th>
<th>Value</th>
</tr></thead>
<tbody>
<tr>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>7</td>
</tr>
<tr>
<td>8</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>9</td>
</tr>
<tr>
<td>A</td>
<td>10</td>
</tr>
<tr>
<td>B</td>
<td>11</td>
</tr>
<tr>
<td>C</td>
<td>12</td>
</tr>
<tr>
<td>D</td>
<td>13</td>
</tr>
<tr>
<td>E</td>
<td>14</td>
</tr>
<tr>
<td>F</td>
<td>15</td>
</tr>
</tbody>
</table>
<p>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: <code>3D16 = 3*16<sup>1</sup> + 13*16<sup>0</sup> = 48 + 13 = 61</code>. So 3D16 is equal to 6110. Notice how we use D's value of 13 in our calculation.</p>
<p>We can convert from base-10 to base-16 similar to the way 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 16<sup>2</sup>, or 296. Then:</p>
<ol>
<li>696/16<sup>2</sup> = 2 remainder 184</li>
<li>184/16<sup>1</sup> = 11 remainder 8</li>
<li>8/16<sup>1</sup> = 8 remainder 0</li>
</ol>
<p>We have to replace 11 with its digit representation B, and we get 2B816.</p>
<p>Feel free to try some more conversions for practice. You can use the application below to check your answers:</p>
<iframe src="https://codepen.io/tutsplus/embed/JrLVrr/?height=400&theme-id=12451&default-tab=result&embed-version=2" width="850" height="400" frameborder="no" allowfullscreen="true" scrolling="no"></iframe>
<h2>Binary! (Base-2)</h2>
<p>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:</p>
<blockquote>“There are only 10 types of people in the world: those who understand binary and those who don’t.”</blockquote>
<p>Can you figure out what it means?</p>
<p>Let’s try a few conversions with base-2. First, we’ll convert 1011002 to base-10. We have: <code>101100 = 1*2<sup>5</sup> + 1*2<sup>3</sup> + 1*2<sup>2</sup> = 32 + 8 + 4 = 4410</code>.</p>
<p>Now let’s convert 65 to binary. 2<sup>6</sup> is the highest power of 2 less than 65, so:</p>
<ol>
<li>65/2<sup>6</sup> = 1 remainder 1</li>
<li>1/2<sup>5</sup> = 0 remainder 1</li>
<li>1/2<sup>4</sup> = 0 remainder 1</li>
<li>1/2<sup>3</sup> = 0 remainder 1</li>
<li>1/2<sup>2</sup> = 0 remainder 1</li>
<li>1/2<sup>1</sup> = 0 remainder 1</li>
<li>1/2<sup>0</sup> = 1 remainder 0</li>
</ol>
<p> And thus we get our binary number, 1000001.</p>
<p>Understanding binary is super important. I’ve included a table below to point out digits’ values.</p><figure class="post_image"><img alt="Powers of Two" data-src="https://cms-assets.tutsplus.com/uploads/users/30/posts/9306/image/table.png"></figure>
<p>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.</p>
<hr>
<h2>Tricks and Tips</h2>
<p>Normally, when converting between two bases that aren’t base-10, you would do something like this:</p>
<ol>
<li> Convert number to base-10</li>
<li> Convert result to desired base</li>
</ol>
<p>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 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 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, 5D16.</p>
<p> We can go the other direction also, by converting each hexadecimal digit into four binary digits. Try converting B716 to binary. You should get 101101112.</p>
<p>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:</p><figure class="post_image"><img alt="Base-8 to Binary" data-src="https://cms-assets.tutsplus.com/uploads/users/30/posts/9306/image/table-2.png"></figure>
<p>Of course, you can reverse the process to go from base-8 to binary also.</p>
<hr>
<h2>Conclusion</h2>
<p>Let’s go all the way back and revisit the color guessing game.</p>
<iframe src="https://codepen.io/tutsplus/embed/Qqmozz/?height=600&theme-id=12451&default-tab=result&embed-version=2" width="850" height="600" frameborder="no" allowfullscreen="true" scrolling="no"></iframe>
<p>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. </p>
<p>Try the game again, and see how much better you can do!</p>
<p>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.</p>2012-02-13T07:00:21.000Z2012-02-13T07:00:21.000ZJason Killian