Hex, Binary, Decimal

All tutorials we have thought to write or that have been compiled that do not explicitly belong in another category.
Post Reply
User avatar
weazy
Ex-Admin
Posts: 1688
Joined: Sun Jul 07, 2002 10:02 am
Location: any given
Contact:

Hex, Binary, Decimal

Post by weazy » Fri May 30, 2003 6:08 pm

I've decided to write about a topic that is fun and very useful and one that not many people have mastered. I'm going to explain binary, hexadecimal, and decimal number systems and how to convert between them.

Decimal number system:

The decimal number system is the number system we all know and love. Decimal is a base 10 system, meaning that each place value goes up by a power of ten. This also means that in each place there can only be the numbers 0-9. Once we go past 9 a "1" is placed in the next column to represent 1 * 10^1 and a 0 in the last column to represent 0 * 10^0. Here is a diagram of the place values up to 10,000,000 in a base 10 system:

|10,000,000|1,000,000|100,000|10,000|1,000|100|10|1|

For example, the number 123,289 can also be represented as:

1 * 10^5 + 2 * 10^4 + 3 * 10^3 + 2 * 10^2 + 8 * 10^1 + 9 * 10^0

Binary number system:

The binary number system is a base 2 system, which means that all the place values are a power of 2. In a base 2 system there can only be a "1" or a "0" in any given place because once a number is more than 1 a "1" is placed in the next column to represent 1 * 2^1 and a "0" is placed in the last column to represent 0 * 2^0. Here is a diagram of the place values in the binary system up to 128:

128|64|32|16|8|4|2|1

For example, in binary, the number 101001 can also be represented as:

1 * 2^5 + 0 * 2^4 + 1 * 2^3 + 0 * 2^2 + 0 * 2^1 + 1 * 2^0


Hexadecimal number system:

The hexadecimal number is base 16. This means that all the place values in a hexadecimal number go up by a power of 16. In any place there can be the numbers 1-9 and also the letters A-F. In a hexadecimal number the letters A-F represent the numbers 10-15.
A=10 B=11 C=12 D=13 E=14 F=15

Here's a diagram of the place values in the hexadecimal up to 268,435,456:

268,435,456|16,777,216|1,048,576|65,536|4,096|256|16|1

For example the number 45A in hexadecimal can also be represented as:

4 * 16^2 + 5 * 16^1 + 10 * 16^0


Converting between number systems:

To give an example of converting numbers between the decimal hexadecimal and binary number systems I'll convert a number from hexadecimal to binary and then to decimal.

Lets take the hexadecimal number 4D, which happens to be the ASCII code for the letter "M":

Converting binary numbers to hexadecimal and back is made to be easy; in fact hexadecimal is a shorthand way of writing binary numbers. Each digit in a hexadecimal number represents 4 binary digits or bits. In order to convert 4D into binary we can first separate the 4 and the D and say that they each represent a 4-bit number. The D represents 13. In order to convert that to a 4bit binary number we have to look back at our binary place diagram and see what combination of place values will add up to 13. These are 8 + 4 + 1. So...
D = 1101
The "4" is simply 4 which is 100 in binary, but because we said that each hexadecimal digit is equal to 4 bits we add a 0 to the front to make it a 4 bit number. So...
4 = 0100

Now all we have to do is put them together:

0100 1101 = 01001101
4 D

In order to convert the binary number 01001101 into a decimal number all we have to do is take each "1", multiply it by its place value, and add the results together:


128|64|32|16|8|4|2|1
0 1 0 0 1 1 0 1

64 + 8 + 4 + 1 = 77

SO... 4D hex = 01001101 bin = 77 dec

I hope I've left you utterly confused; confusion is one of the first steps to enlightenment. If not, I hope I've taught you something new or at least refreshed you on a very important concept in the world of computers.
--The Devil is in the Details--

Post Reply