I am currently working on taking a few courses to get certified in various programming languages. At the moment, I am enrolled in a Zend PHP certification course, and have learned quite a bit even though I’m only halfway through the course.
The main concept I’ve learned is that, although I knew quite a bit about the standard PHP functions, and although I know a good bit about programming specifically for the Web, I know very little about traditional programming. Having never learned C, BASIC, etc., I have no base of information that a lot of other, more experienced programmers have.
I plan to explain how useful binary numbers can be in programming, and how often they are overlooked by people programming for the Web. To put it simply, most of us Web programmers (and I am basing this off of both opinion, and a very small sample of the programming population—the rest of the people in my class), simply have never needed to use binary numbers because we’ve never programmed in assembly languages. They can be extremely helpful, though, and incredibly efficient.
In this article, I’m simply going to attempt to tackle the concept of binary numbers, as there are a lot of people in the world that don’t really understand how they work or why they exist. Being that everything in the world of computers actually eventually resolves to binary numbers in some way or another, they are actually extremely important.
Binary numbers are all of the numbers you can create from adding exponents to the number 2. You begin with 20, which equals 1. Then, 21 equals 2, 22 equals 4, 23 equals 8, etc., etc., etc.
To explain a little better, I will show you a little chart:
27 (128) |
26 (64) |
25 (32) |
24 (16) |
23 (8) |
22 (4) |
21 (2) |
20 (1) |
|
---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
In the chart above, I am showing an 8-bit number, basically. The individual switches shown above (the zeros shown in the second row) are called “bits”.
Every single number you can think of can be built using binary. What’s more, each number can only be built one way in binary.
As a side note, the table above has been split into 4-column groups, as that makes it easier to read the information. Generally, binary numbers are split into either 4-column groups or 8-column groups in order to make it easier to read them.
To build a number in binary, you simply turn switches (or bits) on and off. The number one means that the switch is turned on, a zero indicates that the switch is turned off. You then find all of the bits that are turned on, and you add them together. For instance, if the “128” column is turned on and the “1” column is turned on, and all of the others are turned off, then you have 128+1, which is 129. In binary, the number shown above is the number zero.
To build the number 173, we would turn on various switches like this:
27 (128) |
26 (64) |
25 (32) |
24 (16) |
23 (8) |
22 (4) |
21 (2) |
20 (1) |
|
---|---|---|---|---|---|---|---|---|
1 | 0 | 1 | 0 | 1 | 1 | 0 | 1 |
The highest 8-bit number is 255, which is the number you get by turning all 8 bits.
For those of you that have never seen binary before, you might start recognizing some of the numbers I’m showing you. For instance, have you ever wondered why your color values top out at 255 each when you’re trying to build a color from the RGB model? That’s why. Each color in the RGB model (the colors, by the way, are red, green and blue) is stored as an 8-bit binary number. By enabling exactly the right number of bits, you blend the red, green and blue together to get the right shade of whatever color you want.
Have you ever wondered why hard drives, memory sticks, etc. all come in weird sizes? Have you ever wondered why a kilobyte is 1,024 bytes (or why a megabyte is 1,024 kilobytes) instead of just an even 1,000? To put it simply, a kilobyte is 210 bytes, which resolves to 1,024. There are actually 8 bits in a byte, so, in a kilobyte, you are dealing with essentially 1,024 separate 8-bit numbers. Everything you do on your computer eventually gets converted into 8-bit numbers in some way, shape or form, and then each 8-bit binary number is stored in a single byte of information on your computer.
To be perfectly honest, not only is it possible to generate every number in binary, it’s actually possible to create every alpha-numeric character and every color through binary. That’s how computers store information, as described above. Characters and colors are all converted into binary numbers before they are stored on your computer. A picture is really a collection of colored pixels, to which each has been assigned an 8-bit binary number and is stored in huge text file that would make absolutely no sense to most people if they opened it.
Why am I explaining all of this? Well, the answer is quite simple: everything on your computer (whether you’re programming for an operating system or a Web-based application) resolves into binary code.
When you deal with functions, even functions as simple as arithmetic, you are essentially going through a complicated process. Let’s look at how a computer would perform a function as simple as addition.
To add 3+5, your computer would do something similar to the following:
- Take the number 3, convert it to 0000 0011 in binary
- Take the number 5 and convert it to 0000 0101 in binary
- Compare the two numbers and resolve them to 0000 1000
- Convert the answer back to the numeral 8
Imagine how much more complicated it can get when you are dealing with complex functions.
Why should we let the computer use all of those extra resources when we could save it a few steps. That’s what you can do by using some simple binary operations. In my next article, I will explore one of the most useful, under-utilized binary operations known as “bit stuffing” or “bitwise operators”.
I think a lot of people will find bitwise operators extremely helpful when writing programs for the Web.
You can find a lot more information on binary numbers, how they are used in binary files and much more on Wikipedia.
4 Responses
ok im lost :)
Yeah, unfortunately binary numbers are a bear to really grasp (and apparently to explain, too :)).
The worst part is, that’s one of the easiest parts to of the class to understand. :(
I still dont get this. :-(
Good introduction article. Be careful though when you say that “it possible to generate every number in binary”. This is only true for fixed point numbers. As you go with floating point numbers (single (32-bit) and double (64-bit) precision), most decimal numbers when converted are rounded to their closest binary representation and thus you have a tiny error.