Number Systems: An Introduction to Binary, Hexadecimal, and More [PDF]

Feb 13, 2012 - You probably already know what a number system is - ever hear of binary numbers or hexadecimal numbers? S

3 downloads 17 Views 790KB Size

Recommend Stories


Chapter 2 Binary Values and Number Systems
The butterfly counts not months but moments, and has time enough. Rabindranath Tagore

[PDF] Read An Introduction to Database Systems
Those who bring sunshine to the lives of others cannot keep it from themselves. J. M. Barrie

[pdF] Download An Introduction to Database Systems
Nothing in nature is unbeautiful. Alfred, Lord Tennyson

[PDF] Download An Introduction to Database Systems
The butterfly counts not months but moments, and has time enough. Rabindranath Tagore

An Introduction to Systems Thinking
Ask yourself: How confident are you in your abilities to make decisions for yourself? Next

An introduction to PDF
Those who bring sunshine to the lives of others cannot keep it from themselves. J. M. Barrie

NUMBER SYSTEMS AND CODES [PDF]
Octal system has certain advantages in digital work because it requires less circuitry to get information into and out of a digital system. Moreover, it is easier to read, record and print out octal numbers than binary numbers. Hexadecimal number sys

[PDF] Introduction to UAV Systems
Just as there is no loss of basic energy in the universe, so no thought or action is without its effects,

[PDF] Introduction to Mechatronics and Measurement Systems
Live as if you were to die tomorrow. Learn as if you were to live forever. Mahatma Gandhi

[PDF] Introduction to Systems Analysis and Design
Ask yourself: When I'm in physical or emotional pain, what are some of the best things I can do for

Idea Transcript


Envato Elements

How-To Tutorials

Courses

eBooks

Envato Studio

Community

Pricing

Forum

Help

Subscribe

Sign In

Search Envato Tuts+ Advertisement

CODE > CODING CONCEPTS

Number Systems: An Introduction to Binary, Hexadecimal, and More by Jason Killian 13 Feb 2012

Length: Long Languages: English

FEED

LIKE

FO LLO W

FO LLO W

Weekly email summary

Coding Concepts

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.

Introduction: What is a Number System?

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

Email Address

Update me weekly

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

Advertisement

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

Translations Envato Tuts+ tutorials are translated into other languages by our community

You may be wondering how colors relate to number systems. In short, on a

members—you can be involved too!

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

Translate this post

can be represented in multiple ways using different number systems. Powered by

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 Advertisement

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.

HTML

CSS

JS

ED I T ON

Result

WATCH ANY COURSE NOW Subscribe to Access

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

Visualizing Base-10 using blocks

Use the tool below to output a number into its composite “groups”.

HTML

JS

ED I T ON

Result

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

There are 3 groups of a hundred. Again, not coincidentally, 100 = 10*10 or 102 . 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.

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.

HTML

JS

ED I T ON

Result

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. (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 baseten”.) 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 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 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 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 = . So, 478 cookies is the exact same as 3910 cookies. Jane’s offer seems

3910

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:

Converting 523 from base-8 to base-10

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: 1. 150/82 = 2 remainder 22 2. 22/81 = 2 remainder 6 3. 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 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 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, or course, follow this pattern with any

amount of digits in our number.

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: Base 16 Digit

Value

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

A

10

B

11

C

12

D

13

E

14

F

15

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 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 162, or 296. Then: 1. 696/162 = 2 remainder 184 2. 184/161 = 11 remainder 8 3. 8/161 = 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:

HTML

CSS

JS

ED I T ON

Result

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:

“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 base10. 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: 1. 65/26 = 1 remainder 1 2. 1/25 = 0 remainder 1 3. 1/24 = 0 remainder 1 4. 1/23 = 0 remainder 1 5. 1/22 = 0 remainder 1 6. 1/21 = 0 remainder 1 7. 1/20 = 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. Advertisement

Tricks and Tips Normally, when converting between two bases that aren’t base-10, you would do something like this: 1. Convert number to base-10 2. 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 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. 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. 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.

HTML

CSS

JS

ED I T ON

Result

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.

Misumi USA Get Fast Shipping & Better Pricing. Misumi. Empowering Engineers! us.misumi-ec.com

Advertisement

Jason Killian N/A

Advertisement

LOOKING FOR SOMETHING TO HELP KICK START YOUR NEXT PROJECT?

Envato Market has a range of items for sale to help get you started.

WordPress Plugins

PHP Scripts

JavaScript

From $4

From $1

From $2

Unlimited Downloads From $29/month

Over 9 Million Digital Assets

Hire a Freelancer

Everything you need for your next creative project.

Choose from 2,000 professionals ready to do the work for you.

Get access to over 400,000 creative assets on Envato Elements.

ENVATO TUTS+

JOIN OUR COMMUNITY

HELP

About Envato Tuts+

Teach at Envato Tuts+

FAQ

Terms of Use

Translate for Envato Tuts+

Help Center

Advertise

Forums

25,163

1,089

18,687

Tutorials

Courses

Translations

Envato.com Our products Careers © 2018 Envato Pty Ltd. Trademarks and brands are the property of their respective owners.

Follow Envato Tuts+

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.