Thursday, March 08, 2012

The Binary Number System


The Binary Number System

An excerpt from "The Sydnie Emails", written Jan 29, 2008
Copyright (c) 2008, Kevin Farley"On The Binary Number System"



Who? What? When? How? What do you want to know? It can be short or long, simple or overly complex. Let's try "overkill".
The basis of using the binary numbering system in computing is simple. At the lowest element of a computer you have a switch that is either on or off. If it is off, we call that "0". If it is on, we call that "1". Those are the only values we can count with, 0 and 1. Since there are only 2 values, all computer operations at the lowest level use a number system based on 2, called a base 2 number system, or simply "binary". The term "binary" simply means "two values" in this case.
In the binary number system, there are only 2 values, 0 and 1. To get any number you have to add combinations of 0s and 1s. The key to achieving this miracle sum is that you need to multiply the 0 or 1 by the right multiplier, which in binary will be the value 2 raised to an exponent just as in decimal (base 10) where the multiplier is the value 10 raised to an exponent.
This is why you see strings of 0s and 1s all over the place in computer programming. However, it is more common to see hexadecimal (base 16) but that is another story entirely.
So for example the decimal number 12 in binary would be 1100.
1*2^3 + 1*2^2 + 0*2^1 + 0*2^0
The value 37 in decimal when translated to binary is 100101.
1*2^5 + 0*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0
By using a binary number system, the computer can make zillions of simple "yes/no" decisions that translate into certain values. It is this basic premise that all computers operate on.
One of the reasons why this is so key to computing is that at the heart of every digital processor, you have what is effectively a transistor that can switch an electric current from on to off or from off to on. By manipulating these transistors in patterns using circuits that compare and sum them, you can create a resulting pattern of electric current that represents a value.
Its just like cavemen laying out clam shells to count with, its just cooler now using electricity and lasers. And we have Geico.
Also note that most modern computers have normalized on the standard of using 8 binary digits (called bits) as the basic unit of storage/processing. What this means is a single "byte" of memory is essentially composed of 8 transistors created directly in silicon.
So what this means is that value 12 in decimal is normally represented in binary as 00001100 in computer bits and that would produce an electric current pattern in a typical PC's CPU of :
0v | 0v | 0v | 0v | 3.3v | 3.3v | 0v | 0v
If I were to add a positive current (3.3v) into the left most transistor it would yield the pattern:
3.3v | 0v | 0v | 0v | 3.3v | 3.3v | 0v | 0v
Which in binary is 10001100 which is equal to 140 in decimal. This is equivalent to saying "add 128 to 12" (because we are adding 10000000 to 00001100).
Note that in the CPU, these bits are stored and moved around as electric current. However because of the nature of binary systems (on/off values), you can use other ways to represent it. For example, on a typical hard disc drive, there is a metal platter (or non-metal platter coated in metal) that uses magnetic alignment (north/south) to represent 0 and 1. Also on a compact disc or DVD, each bit is represented as a "pit" or a "land", meaning "the laser light is lost in the pit" and "the laser light is reflected off the land".
It could also be "clam shell means 1" and "no clam shell means 0" to those Geico cavemen.
The importance of this all or nothing concept is that computers work because they have to very quickly measure these electric currents, magnetic directions, and laser reflection accurately (but not clam shells). It is much easier to determine if an electric circuit is at 3.3 volts versus 0 volts while it is much harder to determine if the circuit is at 3.3 volts versus 2.9 volts. The same goes for magnetics and light.
So because computers operate on the on/off electric circuit principle (simply put but it is enormously more complex in reality), it is extremely convenient to represent these electric patterns as sequences of 0s and 1s. It would be really verbose and annoying to have to say "on, off, off, off, on, on, off, off" when you really want to convey the meaning of "140".
Well that is a start let me know what else is needed.
And since I feel like it, and because I really do like math, lets have a little number theory to explain how those sequences of 0s and 1s work.
Here is a short number theory explanation, you can translate, and it makes most sense to describe it in decimal first as most people get blown away when you jump right to the alternate base numbering system.
A starting point. Regardless of all human experience, in mathematics, you do not start counting at "1", you start counting at "0". This point needs to be understood as the "first" instance of anything is instance number 0, not 1, regardless of what you think. Imagine counting your fingers, start with 0 and proceed to 9, you have 10 total fingers, but the first one is #0 and the last one is #9. This is actually an important aspect to remember.
In all number systems, each digit represents a value taken to a base number raised to an exponent. That sounds complicated but its not. When we count in decimal, which is what most normal humans do, the base is 10 (which coincidentally - not - we have 10 fingers). The right most digit is position "0", and then you go up by one as you move to the left.
So for the number "763" the 3 is in position "0", the 6 is in position "1", and the 7 is in position "2".
So to come up with the value of the number, you multiple the digit value by the base number raised to the exponent of its position, starting with position 0.
total_value = digit * base^0 + digit * base^1 + ... + digit * base^n
(where the ^ means "take the base to the exponent of")
So then the value 763 means "7 * 10^2 + 6 * 10^1 + 3 * 10^0"
Remember that a number raised to the power "0" is equal to 1, by definition. Always. That's just how it is.
So we have  7*10*10  + 6*10 + 3*1 = 700 + 60 + 3 = 763
This is the basis of base-N number theory and can be applied to any base value. If your base value exceeds 9, then you run out of digits and you substitute other letters or symbols. For example, in hexadecimal numbering, you use 0-9 for the first 10 digits, then a-f for the next 6 digits for values 10 through 15. Literally, in hexadecimal the digit "f" is equal to decimal value 15, always, forever, by definition.
Now when counting in decimal and you get to value "9", then next value is "10", but notice that it is no longer a single digit, but two digits. What we often take for granted is "10 comes after 9", but in mathematics, 10 is a linear progression from 9 using a systematic value increase that follows rules.
So what you really do when you go from "9" to "10" is you have to add a digit to the left that is now multiplied by the base raised to the next power of the base, which is 10. This means you have the following:
10 = 0 * 10^0 + 1 * 10^1 = 0 * 1 + 1 * 10 = 0 + 10 = 10
Ok, now that we have discussed the basics of base-n numbering systems, lets apply that to binary numbering systems.
In binary numbering systems, the base number is 2, which means each digit is multiplied by the value 2 raised to the power of the digit position, starting with 0. Now also, since by definition of being a base 2 number system, we start counting from 0 which leads to 1 and then to 10, not 2.
The reason there is no 2 is because you never have the base number in your numbering system.  Count your fingers starting from 0 and you end at 9, not 10, but you have 10 fingers. So then in the decimal numbering system you have 10 values (0-9) and in binary you have 2 values (0-1).
So if we take a modest decimal value, say 37 and convert it to binary, lets first look at it in decimal.
37 = 7 * 10^0 + 3 * 10^1 = 7 * 1 + 3 * 10 = 7 + 30 = 37
Now lets do figure out what it is in binary.
For starters, the digit positions and exponent values we need to consider are:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
Now we can stop, because 64 is already greater than the number to convert. In other words, we don't need any multiple of 64 to come up with the value 37.
So starting with the largest power of 2 that does not exceed the number and working down, we can do some simple math. The largest value that is a power of 2 that is less than or equal to 37 is the value 32 which is 2^5. Remember, its exponent is 5 and that means it is digit "5" when counting from 0. So it is the 6th digit (numbering the digits right to left). Take the number to convert (37) and subtract the largest power of 2:
37 - 32 = 5
So we have a remainder of 5. Find the largest power of 2 that does not exceed 5. That would be the value 4 which is 2^2. Since the exponent is 2, there is a 1 put in digit "3". Again, take the difference and find the remainder:
5 - 4 = 1
Once you get a remainder of 0 or 1, you are done. There are no more powers of 2 to consider at that point. The remainder of 0 or 1 becomes the right most digit, which is digit 0. Just use that value for the digit.
So we used the exponents of 5 and 2 and had a remainder of 1. This means that digits 5, 2, and 0 each need to be value 1 while the other digits (1, 3, 4, and all digits beyond 5) need to be value 0. This gives us:
1 0 0 1 0 1
| | | | | |
| | | | | +-> digit 0, comes from remainder
| | | | +---> digit 1, uses no exponent hence 0
| | | +-----> digit 2, uses exponent of 2, hence 1
| | +-------> digit 3, uses no exponent hence 0
| +---------> digit 4, uses no exponent hence 0
+-----------> digit 5, uses exponent of 5, hence 1
Therefore the decimal number 37 is represented as 100101 in binary.
To convert it back, just multiply the digit by its position multiplier and sum up all the values.
1*2^5 + 0*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0
which is
1*32 + 0 + 0 + 1*4 + 0 + 1*1
which is
32 + 0 + 0 + 4 + 0 + 1
which is
37
And that is the basis of base-n number systems with examples in decimal (base 10) and binary (base 2).
Overkill is underrated. :-)

No comments: