The magazine of the Melbourne PC User Group

Coming to grips with binary code
Tom Coleman

We all know that computers use binary, and communicate using strings of ones and zeros. However, just how it is done seems to belong in the inner sanctums of computer gurus. There is a huge difference between clicking on an icon, or using a DOS command (like DIR) and 10101101. Even

MOV DX,0109
MOV AH 09

that is supposed to be as close as dammit to way the computer works, still seems a long way from zeros and ones.

Its all tied in to gates (no relation to Bill). Gates are electronic logic devices that have one or more inputs and one or more outputs. Take a gate with two inputs; depending on how it is set up it it will output something or nothing if you send a pulse of current to one or both inputs.

To use them all we have to do is know whether to send a pulse of current to an input or not to send one. So the clever people who design computers organise the machinery to turn the current on or off to an input depending on what is appropriate. When the current is on we call it one, and when it is off we call it zero.

If we had eight inputs and sent a signal to each simultaneously - for example, 10101101 - we might be turning on or off eight switches, or perhaps two lots of four switches.

Suppose those switches were associated with your video controller card. They might be turning on the controls of a single pixel. Lets say, on/off, bright/normal, flashing/steady, reverse video. Other switches might control the red, blue, and green signals and the last one housekeeping functions.

That is not exactly how it works, but will do for the purposes of the example, which is to get the principle across.

A byte is eight bits and a bit is a one or a zero. So the above byte might control a pixel that is red and blue on, green off, which is purple, and it could be bright, flashing, and not reverse video.

Naturally, it gets a lot more complex than that, but you get the general idea.

"Ah ha!" I hear you cry, "I read that it is a system of counting and that's certainly not counting." True. You can use a binary system of counting that, when written, looks remarkably like machine code ones and zeros. Let's see how it is done.

We have established that 1 = on and 0 = off. So if I had a torch or a light switch I could send you two signals. That's all. I have only two options. Get more torches or have an agreement to send signals in groups. A bit like morse code, only using on and off instead of long and short. This suggests a problem. How can you tell if the signal is off or that the battery has gone flat?

Computers solve that by sending a high and a low signal. If the signal is completely off there is a power failure. To avoid that problem I will assume I have several torches, all in good condition.

With one torch I can send two signals, on or off. Now if I have two torches I can send four signals. With the first off, the second can be on or off, that's two. Then with the first on I can again have the second on or off. That's another two, making four.

If I add a third torch I can add another on and off to each of the previous four signals making a total of eight, and so it goes. I can double the number of previous signals by adding another torch. You can work it out that I can make 256 signals with eight torches.

If I want to indicate which of the torches are on or off I can put it like this: 11001010, or some such.

Looks familiar? Obviously if I can have 256 signals then I should be able to use them to count up to 256. So I can, almost. You see, in computing zero is a number, so I can count up to 255 which is 256 numbers including zero.

Let's take a small diversion. The way we normally read numbers is based on the position of the number. Each position, reading from the right, is ten times greater than the previous position. In other words the first position is the number of ones, the second the number of tens, the next the number of hundreds and so on.

Thus the number 1234 can be read as four ones plus three tens plus two hundreds plus one thousand. That's not particularly useful, but it gives us a system for reading binary numbers. Take the earlier number, 10101101. It's the same as reading regular numbers except that each position is only twice the value of the previous position instead of ten times. The first position on the right is one. Moving left doubles the value for each position.

Using the same method, the above byte becomes, one 1 plus no 2 plus one 4 plus one 8 plus no 16 plus one 32 plus no 64 plus one 128. More conventionally it would look like this:

 
128+0+32+0+8+4+0+1 = 173

Looking at it that way suggests an immediate short cut. Don't bother with the zeros. Just put down the position values where there is a 1. So the above could be shortened to:

 
128+32+8+4+1 = 173

Here is another one: 10100101, which works out to:

 
128+32+4+1 =165

One last example: 00110011 translates to:

 
32+16+2+1 = 51

You can make up your own practice bytes as well as I can. You don't need me for that. Just string together eight bits and have a go. You will soon get the hang of it.

Reprinted from the September 1996 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]