The Decimal, Binary, and Hexadecimal Numbering Systems
Basics of Numbering Systems
There are many different numbering systems, each one based on a
number of symbols. In assembly language programming, we are
interested in more the one numbering system. We normally work
with decimal. The decimal system has 10 symbols. The computer,
on the other hand, normally works with the binary system, which
only has two symbols. The computer is real good with binary,
but humans are not, so we use a "shorthand" of the hexadecimal
numbering system for human representation of binary values.
Hexadecimal number system has 16 symbols. To make sure that there
is no confusion about which base we are using, we will assume
that in the absence of other indications, it is decimal.
We use a subscript for the base for all other systems:
10, 102, 1016
See how it could be confusing without a system of notation!
Each symbol represents a unique value. You are
familiar with the decimal system, using the symbols 0, 1, 2, 3, 4,
5, 6, 7, 8, and 9. The decimal system is based on the powers of
10.When we want to represent a value greater than 9, we use a
compound system, where the right-most symbol is one power of
ten lower that its neighbor on the left.
Decimal System
Thus, we have the number 397 is really:
3 times 100 102 300
9 times 10 101 90
7 times 1 100 7
---
397
These are mathematical concepts we use daily, even if we do not
think about them when we use those concepts.
Binary System
The computer uses a different numbering system. One that is
based on two symbols (which also is based on two states, on and
off, just like the light switch on the wall.) The two symbols
are 0 and 1. In this system, everything
is based on a power of two.
The most common powers of two and values (which are the ones you
are expected to know) are:
20 | 1 |
21 | 2 |
22 | 4 |
23 | 8 |
24 | 16 |
25 | 32 |
26 | 64 |
27 | 128 |
28 | 256 |
29 | 512 |
210 | 1024 |
211 | 2048 |
212 | 4096 |
213 | 8192 |
214 | 16384 |
215 | 32768 |
216 | 65536 |
To convert a binary number to decimal, simply add up the decimal
equivalents of the positions in the value that are
non-zero This leads to 10112 being converted as:
1 times 8 23 8 (decimal)
0 times 4 22 0 (decimal)
1 times 2 21 2 (decimal)
1 times 1 20 1 (decimal)
---
11 (decimal)
Examination of this example should reveal that we used the same
concepts, but substituted two for ten as the base.
To convert a decimal number to binary, we can use subtraction
to reverse the process. (Note: There are other methods, but
this only involves adding and subtractings.)
First, find the largest power of two that does not exceed the
value to be converted. If we wish to convert the number
24010 to binary, we can see from the chart the we are
looking for 12810 or 27. That means that
there are eight positions in the binary equivalent (have to
account for 20) and the left-most is set to 1.
1 _ _ _ _ _ _ _2
Now subtract 128 from 240.
240
-128
----
112
And Continuing
Now the next power of 2 is check to see if it can be subtracted
without the answer being a negative number. If it is possible,
we put a 1 in the binary result, otherwise we put a 0.
This continues until the last position is filled.
Well, 64 can be subtracted, so:
112 11 _ _ _ _ _ _2
- 64
----
48
32 can be subtracted, so:
48 111 _ _ _ _ _2
- 32
----
16
16 can be subtracted, so:
16 1111 _ _ _ _2
- 16
----
0
8 can not be subtracted, so:
0 11110 _ _ _2
4 can not be subtracted, so:
0 111100 _ _2
2 can not be subtracted, so:
0 1111000 _2
1 can not be subtracted, so:
0 111100002
Hexadecimal System
When working with large binary values, humans have a tendency to
make mistakes, so we use a different numbering system which is
more convenient to use, the hexadecimal numbering system.
Hexadecimal means sixteen. So, what sixteen symbols are used for
this? Simple, use the ten from the decimal system and add the
first six letters of the alphabet: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
A, B, C, D, E, and F.
This leads to FE08:
F times 4096 163 61440 (decimal)
E times 256 162 3584 (decimal)
0 times 16 161 0 (decimal)
8 times 1 160 8 (decimal)
-----
65032 (decimal)
We can compare the first sixteen binary, decimal, and hexadecimal values in the following table:
Binary | Decimal | Hex |
0000 | 0 | 0 |
0001 | 1 | 1 |
0010 | 2 | 2 |
0011 | 3 | 3 |
0100 | 4 | 4 |
0101 | 5 | 5 |
0110 | 6 | 6 |
0111 | 7 | 7 |
1000 | 8 | 8 |
1001 | 9 | 9 |
1010 | 10 | A |
1011 | 11 | B |
1100 | 12 | C |
1101 | 13 | D |
1110 | 14 | E |
1111 | 15 | F |
One of the things to note, the value 0000 is exactly the same
value as 0. Leading zeroes do not alter a value. When working
with binary and hex values, it is common to write them as
8-, 16-, or 32-bit values by adding the necessary leading
zeroes.
Hexadecimal to Binary
Hex to binary and binary to hex are the simplest, since you only have to worry about sixteen possibilities! To convert FE08 to binary, look up each hex digit and put in the four bits that are the same value:
F E 0 8
1111 1110 0000 1000
Binary to Hexadecimal
This is the reverse of Hex to Binary. To convert 1111111000001000, first divide the number into groups of four bits, starting from the left side:
1111 1110 0000 1000
Then convert each four bit group to hex:
1111 1110 0000 1000
F E 0 8
Hexadecimal to Decimal
Forget it! Convert hex to binary and then binary to decimal.
Trust me, it is easier! (Calculators are handy for this also!)
Previous |
Next
©2004, Gary L. Burt