“The greatest enemy of knowledge is not ignorance; it is the illusion of knowledge” – Stephen Hawking
While writing a computer program or what some cool people say coding, I think the most frequently used statements is addition and subtraction. Say I write a = 2+3, so the variable ‘a’ will store value 5 in it. How does a computer performs this calculation? The most common answer that I got was something like “computers work with binary numbers”. It’s like I asked “How to make coffee?” and you answer “We do cooking”. Let us look at the recipe of calculation, inside a computer.
Humans work on numbers in decimal format (the numbers 0–9) largely, it’s believed, because we have ten fingers and toes to count with.
For the last century or so, computers and calculators have been built from a variety of switching devices that can either be in one position or another. Just like a light-switch, they’re either “ON” or “OFF.” For that reason, computers and calculators store and process numbers using what’s called binary code, which uses just two symbols (0 and 1) to represent any number. So in binary code, the number 19 is written 10011 (read it as “one-o-o-one-one”, not “ten thousand eleven”), which means (1 × 24) + (0 × 23) + (0 × 22) + (1 × 21) + (1 × 20) = 19. The beauty of binary is that you can represent any decimal number with a series of switches that are either ON or OFF—perfect for a calculator or a computer—like this:
Representing the decimal number 19 with five binary switches 10011
How to represent the binary number 19 inside a calculator or a computer using five switches
Three are pressed down (switched ON) and two are left as they were (switched OFF), indicating the binary number 010011, which is equal to 19 in decimal. Imagine using a switch with 10 positions to represent 10 digits of our decimal system!
Using Logic Gates with Binary
Now we know about the ingredient of our recipe but still the cooking is incomplete, so let’s start. Let’s say you want to do the sum 3 + 2 = 5.
A computer tackles a problem like this by turning the two numbers into binary, giving 11 (which is 3 in binary) plus 10 (2 in binary) makes 101 (5 in binary). How does the calculator do the actual sum? It uses logic gates to compare the pattern of switches that are active and come up with a new pattern of switches instead.
A logic gate is really just a simple electrical circuit that compares two numbers (inputs) and produces a third number (an output) depending on the values of the original numbers. There are three basic gates called OR, AND and NOT .
- OR Gate
An OR gate has two inputs (each of which can be either 0 or 1) and it produces an output of 1 if either of the inputs is 1, it produces a zero otherwise.
- AND Gate
An AND gate also has two inputs, but it produces an output of 1 only if both inputs are 1.
- NOT Gate
A NOT gate has a single input and reverses it to make an output. So if you feed it a zero, it produces a 1 (and vice-versa).
An adder is built from a number of AND, OR and NOT logic gates contained inside chips that are connected together. You feed into these circuits binary numbers as their input and get a third binary number as your output. So if you feed in the electrical signals 10 and 11, you would get out 101 (2 + 3 = 5).
To subtract 2 numbers, the number to be subtracted (called “subtrahend” if you like to show off!) is “reversed” using NOT gate. To compute “5-2”, the additive inverse of 2 is taken, i.e. -2. The new number is then added to get the difference: 5 + (-2) =3.
Multiplication can be done using multipliers (similar to adders, multipliers are combinational circuits of logic gates). If you are thinking of adding multiple times, it’s fine and correct but is not efficient. It’s like you boil your entire coffee at once and add sugar later. However, if the workload is enormous, like making coffee for the whole city, it’s not most efficient way to do so.
In case of division take multiplicative inverse (reciprocal). For instance, multiplicative inverse of 2 is 1/2. So, 10/2 becomes 10 x (1/2).How does an adder or a multiplier work? That, my curious readers, is a topic for some other day.
Although each step, in itself, is very simple, the entire process is repetitive. All these may seem tiring to you. Thankfully, that’s what computers are good at: repeating a process endlessly. After performing the calculation, the Binary number is converted back to its Decimal form, using logic gates.
Now since you have learned recipe of calculation, for the sake of engineering next article will definitively discuss the stove we use.