Thursday, November 19, 2009

Binary Conversion

To convert a binary number to a decimal number you must first understand what each digit in the binary number means. To explain this let's look at the decimal number 247.

The '2' in 247 represents two hundred because it is a two in the hundreds position (two times a hundred is two hundred). In similar fashion, the '4' in 247 represents forty because it is a four in the tens position (four times ten is forty). Finally, the '7' represents seven because it is a seven in the units position (seven times one is seven). In a decimal number, the actual value represented by a digit in that number is determined by the numeral and the position of the numeral within the number.

It works the same way with a binary number. The right-most position in a binary number is units; moving to the left, the next position is twos; the next is fours; the next is eights; then sixteens; then thirty-twos ... Notice that these numbers are all powers of two - 2^0, 2^1, 2^2, 2^3, 2^4, 2^5. (The units, tens, hundreds, thousands, ten thousands of the decimal system are all powers of ten: 10^0, 10^1, 10^2, 10^3, 10^4).

So, to convert the binary number 1001 (don't read that as one thousand one - read it as one zero zero one) to decimal, you determine the actual value represented by each '1' and add them together. The right-most '1' has a decimal value of 1 (it is in the 2^0, or units, position) and the left-most '1' has a decimal value of 8 (it is in the 2^3, or eights, position). So the binary number 1001 is equal to
decimal 9. Here's another way to look at it:


1 0 0 1
^ ^ ^ ^
| | | |_________> 1 x 2^0 = 1 x 1 = 1
| | |___________> 0 x 2^1 = 0 x 2 = 0
| |_____________> 0 x 2^2 = 0 x 4 = 0
|_______________> 1 x 2^3 = 1 x 8 = 8
---
9

Both the decimal system and the binary system are positional number systems. The hexadecimal system is another positional number system. The binary system has only two numerals - 0 and 1; the decimal system has ten numerals: 0,1,2,3,4,5,6,7,8, and 9. In the hexadecimal (or hex) system there are sixteen numerals: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E, and F. Zero through nine have the same value as a decimal numeral, and A is ten, B is eleven, C is twelve, D is thirteen, E is fourteen, and F is fifteen. After a while you will get used to seeing "letters" used as numerals!

The decimal number system is also referred to as "base ten" since each position in a decimal number represents a power of ten - a number that can be written as 10^n, where n is an integer. The binary number system is also referred to as "base two" since each position in a binary number represents a power of two - a number that can be written as 2^n, where n is an integer. The hex number system is also referred to as "base sixteen" since each position in a hexadecimal number
represents a power of sixteen - a number that can be written as 16^n, where n is an integer.

The right-most position in a hexadecimal number is units; moving to the left, the next position is sixteens; the next is two hundred fifty-sixes; the next is four thousand ninety-sixes, and so on - all powers of sixteen - 16^0, 16^1, 16^2, 16^3.

To convert a binary number to a hex equivalent, notice that four binary digits together can have a value of from 0 to 15 (decimal) exactly the range of one hex digit. So four binary digits will always convert to one hex digit!

For example:

10110111 = B7 (hex)

The right-most four digits of the binary number (0111) equal seven, so the hex digit is '7'. The remaining left-most four digits of the binary number (1011) equal eleven, so the hex digit is 'B'. Here is another way of looking at it:

1 0 1 1 0 1 1 1 from right to left, make four-digit groups
\ /\ /
\ / \ /
eleven seven determine the decimal equivalent of each
| | group
V V
B 7 write the equivalent hexadecimal digit

What is the decimal equivalent of B7 hex?

B 7
^ ^
| |_________> 7 x 16^0 = 7 x 1 = 7
|___________> 11 x 16^1 = 11 x 16 = 176
---
183 decimal

Check that against the decimal equivalent of 10110111 binary:

1 0 1 1 0 1 1 1
^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | |_________> 1 x 2^0 = 1 x 1 = 1
| | | | | | |___________> 1 x 2^1 = 1 x 2 = 2
| | | | | |_____________> 1 x 2^2 = 1 x 4 = 4
| | | | |_______________> 0 x 2^3 = 0 x 8 = 0
| | | |_________________> 1 x 2^4 = 1 x 16 = 16
| | |___________________> 1 x 2^5 = 1 x 32 = 32
| |_____________________> 0 x 2^6 = 0 x 64 = 0
|_______________________> 1 x 2^7 = 1 x 128 = 128
---
183 decimal

1 comment: