Speaking To Computers
As we discussed in the last post, programming a computer is the process of manipulating the computer to do what you want it to do – to make this generic invention into a specific invention for your task. Programming a computer is nothing more than telling it what to do. So the tricks of programming are:
- Giving the computer clear instructions so it will do what you want
- Translating those instructions into a language the computer can understand
A computer is very much like a well-trained dog: A computer will always do exactly what it is told, if possible. You may be thinking that this is not correct. Actually it is completely correct. Unless what you want the computer to do is impossible (like divide a number by zero), a computer always does what it is told. Where the confusion comes in is that often a computer is not told to do what we thought we told it to do.
What is sometimes frustrating by a computer is that it does a poor job of understanding intent. It takes every instruction literally; it cannot interpret what you really meant to say. So one of the challenges in computer programming is in learning how to express ourselves clearly so a computer will do exactly what we tell it to do.
That’s only half the battle, give or take. Translating those instructions into computer language is the other half. So what, exactly is the language of a computer?
To understand this, let’s digress a bit and talk about Paul Revere’s midnight ride. Prior to April 18, 1775, Paul Revere and Robert Newman had come upon a code whereby Newman could communicate to Revere the movements of the British troops. They agreed to communicate via lanterns: one lantern if the troops were moving in by land, and two lanterns if the troops were moving in by sea.
Let’s use a zero (0) to indicate a lantern that is turned off and a one (1) to indicate a lantern that is turned on. Here is the complete code that Paul Revere and Robert Newman agreed to:
| Lantern 1 | Lantern 2 | Meaning |
|---|---|---|
| 0 | 0 | British Are Not Coming |
| 1 | 0 | British Coming By Land |
| 0 | 1 | British Coming By Land |
| 1 | 1 | British Coming By Sea |
The first code is the obvious choice – no lanterns means nobody is coming. The second and third codes are just reverses of each other. Since Mr. Revere could not determine positionally one lantern from the other, holding up either lantern by itself in either of Mr. Newman’s hands had to signal the same code – coming by land. And, of course, two lanterns meant they were coming by sea.
If you understand Paul Revere’s code, you can comprehend how computer language can be based on switches. In Paul Revere’s code different combinations of switches, or lanterns, being turned on or off meant different things. Other than having different codes, computer language is essentially the same.
The smallest piece of computer information is called a bit, and it represents the state of one switch – off or on. We use the same notation as we just described – a zero (0) means off and a one (1) means on. The smallest logical grouping of bits is called a byte. On most computers today a byte is eight bits. For example, the byte 01000001 could be used to represent the letter A, 01000010 could represent B, etc.
Most of the computers we used in the late 90′s were called 32-bit machines. This refers to the size of an instruction – 32 bits. Most newer computers today have instructions that are twice as big – 64 bits. For example, on the computer I’m typing on right now, it uses 64 bits – 64 switches – to encode every instruction and every piece of data.
Fortunately for us, we don’t have to actually learn the computer’s language, the zeroes and ones a computer uses to understand data and instructions. We instead learn computer programming languages – like C or Python – and let tools translate our instructions, written in a programming language, into ones and zeroes the computer can understand.
And that pretty much leads us into the last point of this topic. In this series of posts we’re going to learn to program using two different programming languages, C and Python. Each language has a different approach to giving instructions to the computer.
The second language, Python, is what is called an interpreted language. We write code in Python, and then we tell the Python interpreter to interpret our code for the computer to tell the computer what to do. This is kind of like going to the market in a foreign country, with a friend that speaks our language as well as the foreign language. As we walk through the market, we tell our friend to communicate a message to the shop owner, and after having done that, the friend relays back to us the results of our communication.
The first language, C, is called a compiled language. Programs written in C are given to a compiler, which translates the program directly into machine language and makes a runnable application out of it – something the computer itself can run on its own. This is like telling our friend exactly what we want from the market and having the friend compile together all our instructions at home first. Then the friend goes directly to the market and does what we wanted.
Both types of languages, compiled and interpreted, offer two different approaches to the problem of converting our instructions into computer language. Later we’ll cover advantages of each approach. For now, the key is to understand what it means when we say that a program is compiled or interpreted.
So we’ve learned basically four things:
- In order to get a computer to do what we want we have to give clear instructions.
- In order to get a computer to do what we want we have to speak to it in its own language.
- Computer language – instructions and data – is basically comprised of codes represented by groups of switches that are all in certain states of being on or off.
- Computer programming languages are translated into computer language, either via compilers or interpreters.
We’ll start learning to program soon. Stay tuned!