Number SystemsDecoded Binary, Decimal, and Hexadecimal Expert Reference Series of White Papers... These are the decimal, binary, and hexa-decimal number systems.. These alternative numbe
Trang 1Number Systems
Decoded
Binary, Decimal, and Hexadecimal
Expert Reference Series of White Papers
Trang 2In many IT-related fields of study, it is extremely valuable to have a good understanding of the number sys-tems that are often encountered For many people, not appreciating things such as binary and hexadecimal is
a stumbling block that keeps them from advancing their knowledge If you cringe a little bit at the mention of these topics, then this white paper is definitely for you
Of course, you may have learned these things many years ago, and you would like a refresher course on num-ber systems Or, perhaps you are tired of not understanding the gobbledygook that appears in file dumps, in routing tables, and in your protocol analysis tools
Whatever your particular reasons for reading this white paper are, I welcome your attention
What You Will Learn
We all have an instinctive feel for what numbers are They are used on a regular basis to identify a particular house on a street, a channel on the television, or how much to pay for an item
Numbers are used in a variety of ways; they are most often used to convey a quantity to someone—twelve chairs, for instance Numbers are also used in verbal codes, such as 10-4 (ten-four) Sometimes they are part of
an address (such as zip codes)
Despite the intended use, numbers are constructed in the same way and follow the same basic set of rules Understanding those rules is the key to advancing your appreciation of numbers
Several systems of numbers are commonly used in computing circles These are the decimal, binary, and hexa-decimal number systems
These alternative number systems have three things in common: a base number (sometimes called a radix); a set of digits or numerals to work with; and a positional notation in which the position of a given digit
affects how it is interpreted
Each number system employs:
• ABase or radix value around which the number system is organized
• A set of Digits or symbols used in forming numbers
• APositional Notation meaning that the position in which a digit appears conveys information about the significance or weight of that digit
George Mays, CISSP, CCNA, A+, Security+, Network+
Number Systems Decoded
Binary, Decimal, and Hexadecimal
Trang 3Figure A Number System Names, Bases, and Digits
Each number system has a name: Decimal, Binary, and Hexadecimal Since “hexadecimal” does not flow trip-pingly off our tongues, we often simply say “Hex” instead
The decimal number system is encountered most commonly This is the number system that everyone learns as
a child As a result, people are most comfortable with decimal
Everything in a digital world reduces to binary form – everything! The information stored on your disk drive is stored in a binary form The information transported across your network is transmitted in a binary form Binary is the frank language of computers And while people may not be as comfortable with binary, comput-ers love it
Hexadecimal was invented to intimidate those who are new to information technologies It is the number sys-tem that is used to confuse students
Just kidding, hex is used as a kind of shorthand for binary Binary numbers tend to be rather long and cumber-some to type, write down, or say So hex is used to convey the same information in a compact fashion It turns out to be very simple to convert from hex to binary – you will learn how
The “base” value for a number system is simply the number around which the system is organized Everything orbits around the value 10 for base 10 (decimal) numbers For example, there are ten digits to work with Notice that base 2 (binary) numbers have only two digits that can be used
You do see the pattern, don’t you? If base 10 has ten digits to work with, and base 2 has two digits to work with, then base 16 must have – how many digits? You guessed it, sixteen They are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A,
B, C, D, E, and F
The sixteen hex digits sometimes leave people scratching their head because we use the symbols A, B, C, D, E, and F as digits These symbols simply represent quantities, as
follows:
These symbols are letters of the alphabet You know that; I
know that However, in hexadecimal numbers, they are digits,
perfectly good digits
Someone might point out to you that there are 3 windows in
the classroom The digit 3 conjures up something in your brain –
the notion of the quantity 3 In like manner, I could note that
there are B desks in the classroom That should make you think
of the quantity 11, hence 11 desks are present
Hexidecimal Digit Decimal Value
Figure B Hex Digit Values in Decimal
Trang 4Base 10
Let’s consider a simple base 10 number, 1585 The number is structured in a very orderly way To begin with, note that the digits all come from the decimal symbol set (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) and that the symbol 5 appears twice in the number
We us the term “positional notation” to reflect the fact
that the 5 appearing in the rightmost position is
interpret-ed differently than the one appearing two positions to the
left That is to say that the position in which a digit
appears has everything to do with the significance or
weight that we attach to it In this case the rightmost 5
implies 5 ones and that to the left implies 5 hundreds
Take a look at Figure C It is obvious to most people how decimal values are organized The rightmost position
is the “ones place.” To the left is the “tens place.” To the left again is the “hundreds place,” and so on Notice that each “place” or position carries a weight 10 times the one to its right (Just what you’d expect? This is the base 10 number system, after all.)
A careful analysis of our example value reveals that the number 1585 is intended to convey to us, as readers
of the number, a quantity that is comprised of 1 thousand, 5 hundreds, 8 tens, and 5 ones
That is pretty simple, right? There are no surprises here This is stuff from the third grade in elementary school But what about the other number systems? They must be different somehow
Base 2
Let us now examine a binary example, 1001 Start by noting that the digits come from the smaller binary sym-bol set, just 0s and 1s And, in a manner similar to the previous example, the symsym-bol 1 appears twice
The rightmost position in a binary number is the ones
place, as it was in the decimal example (See Figure D.)
But as we progress to the left, each position carries a
weight that is 2 times the one on its right Thus we
observe the ones place, twos place, fours place, eights
place, and so on
So in binary values you have only 2 digits to work with, zeroes and ones, and the “places” in the numbers are each weighted 2 times the position to their right That’s why it is called the base 2 numbering system
Analyzing our example value (1001), we discover that this number is intended to convey to the reader a quan-tity comprised of 1 eight and 1 one, or 9 (in decimal)
Base 16
Now consider a hexadecimal example, 12A Just as with the other number systems, the digits come from the allowable symbol set, in this case 0 through 9 and A through F
Figure D Binary Notation Figure C Positional Notation
Trang 5A few observations are in order before proceeding First,
some people seem to think that there is some difference
between a lower case “a” and an upper case “A.” There
is not You will see both cases regularly and the
differ-ence does not matter Second, as noted previously, some
people cringe seeing letters of the alphabet in numbers
Put your confusion aside Just as the
regular-old-every-day digit 5 makes you think of the number of fingers on
your hand, the hex digit C should make you think of the number of doughnuts that come in a package (Of course, a baker’s dozen would be a hex D.)
Consider Figure E The rightmost position is again the 1s place What is different is that as you move to the left, each position carries a weight that is 16 (get it, base 16) times the position on the right So you end up with a progression like 1, 16, 256, 4096, and so on
Our example value then, 12A, denotes 1 two-hundred-fifty-six, 2 sixteen’s, and A ones With a little quick men-tal gymnastics one realizes the A ones means 10 (decimal) ones to mere mormen-tals So this hex value conveys the notion of 256 plus 32 plus 10, or the quantity 298 decimal
Intermediate Summary
You have learned that three number systems are used commonly in IT: decimal; binary; and hexadecimal Also, these number systems are structured in a similar manner They each have a “base” value of 10, 2, and
16, respectively And, each has a limited symbol set (digits) that can be used in constructing numbers For deci-mal the set consists of the digits 0…9, for binary there are only 0s and 1s, and for hex the digits 0…9 and A…F are used
Each of the number systems employs a positional notation in which a given digit’s position within a number dictates the weight that we associate with it The rightmost position is always the 1s place As you move to the left, the positions carry a weight that is the base value times its right-hand neighbor The progressions 1, 10,
100, and so on appear for decimal numbers With binary numbers we see a progression like 1, 2, 4, 8, 16, 32, etc And for hex we observe the progression 1, 16, 256, 4096, and so on
Recognizing the Number System
How do you know whether the value 101 is decimal, binary, or hex? You don’t, unless there is additional clari-fication provided to you
The number system being used is very often conveyed by the context in which you observe the number By default, we tend to assume that a number is decimal unless otherwise indicated But if you were reading a paper on binary masking (whatever that is) you might reasonably assume that the value is binary You get the idea
Mathematicians use subscripts to indicate the number system So if you see 10110then you are dealing with
101 base 10, decimal The value 1012means that this is a binary (base 2) value And, as you would expect,
10116implies a hexadecimal (base 16) value
Figure E Hex Notation
Trang 6Programmers have various notations that you will encounter as well It is extremely common to encounter a number like 0x101 The 0x prefix is C-programmer-speak for a hex constant So this pops up a lot
Assembly language programmers have their own notations For example, in such a program you might
encounter 101h, meaning a hex value Similarly, 101b implies a binary value
Decimal to Binary Conversion
Converting a decimal value to binary is like making change at a cash register Except, instead of using 1, 10, and 100 dollar bills you use “Bin-o-Bucks”* The denominations are 1, 2, 4, 8, 16, and so on
Make yourself a cheat-sheet like this:
How would you “pay” somebody 18 in bin-o-bucks? You would give them 1 sixteen and 1 two Like this:
And since you are not paying out any eights, fours, or ones, just put a zero in those positions to indicate that fact You end up with:
The binary value 10010 represents the decimal value 18 Easy Try it again by converting the value 3710to bin-o-bucks (Here is your cheat-sheet.)
Now check your result To pay out 37 you would use 1 thirty-two, 1 four, and 1 one Get it? 32 + 4 + 1 = 37 You answer should look like:
Do not make this more complicated than it is Conversion from decimal to binary is, in fact, this simple If you practice just a little bit, then you can easily become quite good at this
Binary to Decimal Conversion
Simply extend the change-making analogy that was just described For example, if someone just handed you
an 8 dollar bill and a 4 dollar bill then they just gave you:
And 8 + 4 is 12 So a binary 1100 is the representation for a decimal 12
Trang 7Try this again for yourself The customer just handed you a 64, an eight and a two Here is your cheat sheet:
Your result should look like this:
64 + 8 + 2 = 74 So you just received 74 bucks
Decimal to Hexadecimal
Stick with the change-making analogy Only replace the bin-o-bucks with “Hex-A-Bucks”* The denominations
of the bills are 1s, 16s, 256s, and 4096s Your cheat-sheet looks like this:
4096 256 16 1
If you had to pay out 3510dollars, that would call for the use of two 16s and three 1s
4096 256 16 1
So the value 23 is the hex representation of the decimal value 35
Try your hand at this You need to pay out 2510in hex-a-bucks What do you get? Here is your cheat-sheet:
4096 256 16 1
Check your answer You should have gotten this:
4096 256 16 1
The answer is one 16 plus nine 1s 16 + 9 = 25
Hexadecimal to Decimal
Extend the hex-a-bucks analogy from the previous exercises to convert in the opposite direction, from hex to decimal this time
Start with you hex cheat-sheet:
4096 256 16 1
Trang 8Now, let’s say that you are asked to convert 5416to base 10 Plug 54 into your cheat-sheet You get:
4096 256 16 1
That is five 16s and four 1s 5 x 16 = 80, add the 4 x 1, you get 84 decimal
Can you do this one: 1E16= ?10 Here is your cheat sheet:
4096 256 16 1
That would be 1 sixteen and E ones Recall that the symbol E depicts the quantity 14 in decimal So that is one
16 and 14101s; 16 + 14 is 30 decimal
Hexadecimal to Binary
Hex to binary is very easy Conversion is done using a simple substitution technique Each hex digit converts to four binary digits Here is what the hex digits look like in binary:
Hex Binary Hex Binary Hex Binary Hex Binary
Do you understand where these numbers come from? Let’s take a couple of them and analyze them to be sure that you appreciate what is going on here, starting with the hex 6 What does 616look like in binary?
Notice the 4 and the 2 bit (binary digit) positions are set to 1 The other positions are 0 One 4 plus one 2 equals 6
Or how about the 916table entry? In binary it looks like:
That’s one 8 plus one 1, or 9
Now that you know how this substitution table was constructed, let’s convert a sample hex value, how about B8, to binary Simply write binary 1011 in place of the hex B followed by binary 1000 in place of the 8 You get
1011 1000 binary (I put a space in there for clarity – binary numbers don’t actually have spaces in them.)
Trang 9Hex value to be converted: B8
Substitute 1011 for hex B: 1011 8
Substitute 1000 for hex 8: 1011 1000
Try another example 4F116= ?2 In place of the 4 write 0100, in place of the F write 1111, and in place of the
1 write 0001 You get 0100 1111 0001
Hex value to be converted: 4F1
Substitute 0100 for hex 4: 0100 F 1
Substitute 1111 for hex F: 0100 1111 1
Substitute 0001 for hex 1: 0100 1111 0001
Binary to Hexadecimal
Converting a binary value to hex is similarly easy Just reverse the substitution process described above Working from right to left in the binary value, substitute one hex digit for each group of four binary digits An example is the best way to appreciate this
Binary value to be converted: 10110110
Identify groups of four bits: 1011 0110
Substitute hex 6 for 0110: 6
Substitute hex B for 1011: B 6
If the number of binary digits (bits) is not a multiple of four, then just pad it on the left with additional zeroes For example:
Binary value to be converted: 1010110110
Identify groups of four bits: 10 1011 0110
Pad the left with zero bits: 0010 1011 0110
Substitute hex B for 1011: 2 B 6
Substitute hex 2 for 0010:
Helpful Tools for Working with Number Systems
There is no shortage of tools to help you work with the
number systems Though hardly the best tool, the Windows
Calculator can be useful
On a Windows system, begin at the Start button, choose All
Programs, then Accessories Finally, choose Calculator The
familiar Windows Calculator will pop up You are probably
used to the wimpy balance-your-checkbook mode that
appears above But if you pick the View pull-down menu
and select Scientific, the fancier calculator features are
revealed
Figure F Calculator
Trang 10Toward the upper left of the calculator you will see a
group box that contains four radio buttons: Hex, Dec,
Oct, and Bin The Bin button has been clicked in the
figure above and a binary value entered into the
calculator: 1010110110
Clicking on the Hex button, the calculator converts the binary value to hex: 2B6 You can use this technique to convert between any of the supported number sys-tems If you are curious, Oct stands for the octal, or base 8, number system
Another tool that you might like is my Number Converter You can obtain this from my website: HYPERLINK
"http://www.gwmays.com" www.gwmays.com Just follow the link to Number Converter Version 2 It can be downloaded as a zip file or, more conveniently, as a self-extracting zip, the exe
This tool has only one input field at
the upper left The program
con-verts any number that you enter
here to each of the number
sys-tems that we learned about A
note: the Number Converter
“guesses” what kind of value you
entered; you may have to click the
Type of Input Value button to
cor-rect the Number Converter if it
guesses wrong
Another tried and true tool is a conversion chart The conversion chart on the next page covers all values between 0 and 255 decimal
Figure G Hex Calculator
Figure H Hex Conversion
Figure I Number Converter 2.0