Sunday 20 November 2011

What is binary? in C programming

What is binary?

The binary numbering system is the lowest common denominator in computing. Binary is base 2. Do you remember being taught different numbering systems in elementary or high school? In one of my math classes  in grade school, I was taught base 6. You count 1, 2, 3, 4, 5, then 10, 11, 12, 13, 14, 15, then 20, and so on. At least that’s the way I was taught. In truth, you should count 0, 1, 2, 3, 4, 5, then 10, 11, 12, 13, 14, 15, and so on. By starting with 0, it becomes slightly easier to see the groupings of six digits—hence the six in base 6. Notice that you count from 0 to the number that is one less than the base (you count from 0 to 5 because 6 is the base). After you have counted to 5, you move to two decimal places. If you think about it, our base 10 (decimal) system is similar—you count up to one less than the base (9), and then you go to two digits and resume counting.

In computers, the numbering system is base 2—binary. With base 2, you count 0, 1, then 10, 11, then 100, 101, 110, 111, then 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, and so on. Contrast base 2 with base 6; in base 2, you count from 0 to 1 before going to two decimal places. Of course, the next question is “Why is base 2 used?” The reason is the transistor. The transistor is what makes

modern-day computers possible. A transistor is like a light switch. A light switch has two positions (or states),
on and off. So does a transistor. You could also say that off equals 0, and on equals 1. By doing so, you can count from 0 to 1 using a transistor (or a light switch if you want). It doesn’t seem as though you can do any serious computing with only two numbers (0 and 1), but we’re not finished yet. Suppose that you had a light switch panel that contained four light switches. Although each switch still has only two states, the four switches, when treated in combination, can have 16 unique positions, or 24 (four switches, two states each). Therefore, you can count from 0 to 15 with just four switches, as shown in Table XX.22.

Binary counting.
--------------------------------------------------------------------------------------------------------------
Switches     Decimal Value             Power
-------------------------------------------------------------------------------------------------------------
0                       0
1                       1                             20
10                     2                             21
11                     3
100                   4                             22
101                   5
110                   6
111                   7
1000                 8                              23
1001                 9
1010                10
1011                11
1100                12
1101                13
1110                14
1111                15
-----------------------------------------------------------------------------------------------------------
The table demonstrates three important points: (1) By placing the switches side by side, you can count with them—up to 15 in this case (16 total counts); (2) You can consider each switch as a decimal place, or rather a binary place, just as you can with the decimal system; (3) When each switch is considered to represent a binary place, that switch also happens to be a power of two (20, 21, 22, 23, and so on). Further, notice that in the table where a power of two is shown, the count had to add another binary place.
 
This is the same as the decimal system, in which each time you increase another decimal place, that new decimal place is a power of 10 (1 = 100, 10 = 101, 100 = 102, and so on). Knowing this, you can convert binary numbers to decimal with minimal effort. For example, the binary number 10111 would be (1 ´ 24) + (0 ´ 23) + (1 ´ 22) + (1 ´ 21) + (1 ´ 20), which equals (16 + 0 + 4 + 2 + 1), or 23 in decimal. A much larger binary number, 10 1110 1011, would be (1 ´ 29) + (0 ´ 28) + (1 ´ 27) + (1 ´ 26) + (1 ´ 25) + (0 ´ 24) + (1 ´ 23) + (0 ´ 22) + (1 ´ 21) + (1 ´ 20), which equals (512 + 0 + 128 + 64 + 32 + 0 + 4 + 2 + 1), or 743 in decimal. So what does all this nonsense get us? In the realm of computers, there are bits, nibbles, and bytes. A nibble is four bits, and a byte is eight bits. Do you know what a bit is? It’s a transistor. Therefore, a byte is eight transistors, side by side, just like the four switches in Table XX.22. Remember that if you had four switches (or transistors) grouped together, you could count to 24, or 16. You could have called that a nibble of switches. If a nibble is four transistors grouped together, a byte is eight transistors grouped together. With eight transistors, you can count to 28, or 256. Looking at it another way, this means that a byte (with eight transistors) can contain 256 unique numbers (from 0 to 255). Continue this a little further. The Intel 386, 486, and Pentium processors are called 32-bit processors. This means that each operation taken by the Intel chip is 32 bits wide, or 32 transistors wide. Thirty-two transistors, or bits, in parallel is 232, or 4,294,967,296. That’s more than 4 billion unique numbers!

Of course, this description does not explain how a computer uses those numbers to produce the fantastic computing power that occurs, but it does explain why and how the binary numbering system is used by the
computer.

Cross Reference:

XX.23: What is octal?
XX.24: What is hexadecimal?

No comments:

Post a Comment