Tuesday, October 12, 2010

There are 10 kinds of people in the world...

... a note on binary.

Our normal decimal system is a base 10 system, meaning that each digit represents 10 to a different order of magnitude. We learned in school that there is a ones place, a tens place, a hundreds place, a thousands, place, etc. Each of these places can also be represented by the number 10 raised to an exponent. The ones place is 100, the tens place is 101, the hundreds place is 102, the thousands place is 103, and so on. (This is also true for the tenths, hundredths, and thousandths, places, except 10 is raised to the -1, -2, and -3 powers respectively.

The digits in each place are then multiplied by that base, and added together.

Let’s use 1234 as an example. Here are several different ways of writing it out:

1234 = 1234
(1 x 103) + (2 x 102) + (3 x 101) + (4 x 100) = 1234
(1 x 1000) + (2 x 100) + (3 x 10) + (4 x 1) = 1234
1000 + 200 + 30 + 4 = 1234

Each digit can only be 0 to 9, since 10 or more would carry over to the next place.

Now take those principles and apply them to binary. Binary is a base 2 system, so each place can be represented by the number 2 to an exponent: 20 = 1, 21 = 2, 22 = 4, 23 =8, etc. Instead of ones, tens, hundreds, and thousands places, we have ones, twos, fours, and eights places.

When writing a number in binary, each digit will only be 0 or 1, since 2 or more would carry over to the next place.

In binary, the decimal 2 is written as 10. The 1 digit is in the 21 (twos) place, and the 0 digit is in the 20 (ones) place. Multiplying out like we did before, we see:

10 = 2 (decimal)
(1 x 21) + (0 x 20) = 2 (decimal)
(1 x 2) + (0 x 1) = 2 (decimal).

The decimal 13 would be written as 1101:
1101 = 13 (decimal)
(1 x 23) + (1 x 22) + (0 x 21) + (1 x 20) = 13 (decimal)
(1 x 8) + (1 x 4) + (0 x 2) + (1 x 1) = 13 (decimal)

This can be done using any base. Another common base is 16, referred to as “hexadecimal”. However, in order to use single digits, the decimals 10, 11, 12, 13, 14, and 15 are replaced with A, B, C, D, E, and F respectively. Remember the digits for any base may only go from 0 to one less than the base, so hexadecimal digits go from 0 to 15, or more appropriately, 0 to F.

For practice, solve the following problems and check your answers in the back of the book...

(Just kidding.)