Friday, February 18, 2011

More on binary

I guess before we can talk about how easy math is to perform on binary numbers, we need to first understand a little more about how binary numbers themselves work. We talked earlier about how a byte can range in value from 0b0000 0000 to 0b1111 1111, and how this corresponds to the range of values in decimal of 0 to 255. Let's break it down a little bit more thoroughly to see how binary numbers are constructed.

Decimal numbers are comprised of several digits, or "places" as you may remember them being called in elementary school. We have the "ones place" the "tens place" the "hundreds place" and so forth. So the number 453 means four "hundreds" five "tens" and three "ones." The number 307 means three "hundreds" zero "tens" and seven "ones." Binary numbers work the same way, except instead of having "ones," "tens" and "hundreds" (which you will notice are all numbers that can be described by 10^(x)), we have "ones," "twos," "fours" and so forth (all of those places can be described as 2^(x)). Another big difference between decimal and binary is the range that each digit can take on. In decimal, digits can range from 0-9 (ten different options for the base-ten, or decimal, number system). In binary, digits can only be in the range 0-1 (two different options for the base-two, or binary, number system).

Let's take a quick look at a few examples that might cement the differences between decimal and binary in our minds.

In decimal we have such numbers as:
1 = one
10 = ten
100 = one hundred
1000 = one thousand

In binary those same symbols mean something very different:
0b1 = one
0b10 = two
0b100 = four
0b1000 = eight

Let's look at one more concrete example, the difference between the decimal number 1101 and the binary number 0b1101. In decimal those symbols mean one "thousands" one "hundreds" zero "tens" and one "ones," for a total of one thousand, one hundred and one. In binary those symbols mean one "eights" one "fours" zero "twos" and one "ones," for a total of (8+4+0+1)=thirteen. Each successive place (moving left as you're reading a binary number) means another power of two. Here's a list of the powers of two leading up to 2^8: 1, 2, 4, 8, 16, 32, 64, 128, 256 (remember, those are their decimal representation). Those are all of the powers of two we'll need for our discussion next time on binary arithmetic.

No comments:

Post a Comment