What is a Logic Gate?
Have you ever wondered how our computers transform the electricity they consume into the wonderful things we see and do on the screen? How can we watch a video, store so many recipes, or play our favorite games?
Every digital device is built with a set of chips that store and process information. Although there are many chips, all of these chips share the same building block: Logic Gates.
The Logic Gates are the fundamental piece where the transformation from electricity to data happens.
Logic Gates are the building blocks of any computer, and they take care of all the processes happening within. They are simple electric circuits that take electrical signals and convert them into information that we can manage.
Implementing a Logic Gate constitutes a problem for electrical engineering or physics. Therefore, to computer scientists, it doesn't matter that Logic Gates work with electricity. That's why we can say that Computer Science starts at the logic gates.
Calculator
To learn about the Logic Gates, we will build a calculator with technology present before the 20th century. A simple calculator that can do the following additions: "1 + 1", "1 + 0" and "0 + 0".
The switches in the board are used for each of the numbers we want to add. At the bottom, we have two lightbulbs for the result. One lightbulb represents the number 2 and the other lightbulb the 1. When both are off, the result is 0.
Relays
We need one piece of equipment that was widely used in the 19th century. This piece is the relay.
A relay is similar to a switch. However, instead of switching manually, the relay uses an electromagnet. An electromagnet is a device that becomes a magnet when electricity passes through it. Next to it, there is a lever that enables another circuit. When the electromagnet turns into a magnet, it pulls this other lever and enables the other circuit.
Let’s take a look:
The previous image represents a relay in rest mode. No electrical current goes through the first circuit; therefore, the electromagnet is not pulling the lever from the other circuit.
When the current passes in the first circuit, it triggers the electromagnet that pulls the lever from the other circuit, closing it.
Logic operations
We need a second concept to create our calculator: Logic Operations. In a nutshell, logical operations are true or false statements.
For example:
Is Fry's hair orange? AND Is Fry thinking? YES
Is Fry's hair orange? AND Is Fry eating? NO
This is a logic operation called AND. When both statements are true, the whole expression is true. When one of them or both are false, then the expression is false.
Let’s build the AND logic operation with electric circuits. We need two switches, and the lightbulb must shine only when both switches are on.
The lightbulb is on only when both switches are on. If any of the switches (or both) are off, the lightbulb should be also off:
AND circuit
Nobody in the 19th century thought of using electric circuits or relays to perform logical operations. Yet, we can build an electric circuit with relays to perform the AND operation.
We need to connect two relays in series, one after the other.
Let me show what happens when only one of the switches is on:
When only one is on, the lightbulb is off because no current flows through it.
Let’s try the other switch.
Yet, when both switches are on, the lightbulb is on!
This circuit is a Logic Gate, the AND logic gate.
Logic gates are not drawn with all the inner workings. Instead, different logic gates have different diagrams representing them. For example, the AND logic gate is drawn with a D shape:
Note: This diagram is a simplification without batteries or a source of voltage. It’s used to create more readable diagrams. We are going to use the simplified versions from now on.
OR and NOT
There are two more logical operations (or two more logic gates) that we need for our calculator.
OR means that the lightbulb is on when any of the switches –or both– is on. The following is how to draw an OR. It has the shape of a bow.
Next, we learn the shape of the NOT: a triangle and a small circle.
The NOT is a little weird; when no current comes in, current comes out. When current comes in, no current comes out. Let’s see the two states:
No current in, current out:
Current in, no current out:
Both OR and NOT are Logic Gates.
Calculator circuit
Let’s build our calculator using only the circuits AND, OR, and NOT. Remember that inside them, there are only relays and electric current.
Note: A dot in the line means that we are splitting the current. When two lines cross without a dot means that they don’t touch each other.
Let me prove how this circuit performs the calculations we want. In the previous diagram, we can see what happens when none of the switches is on.
Next, when only the switch on the left is on (1 + 0), the lightbulb of 1 is on.
The switch on the right is on (0 + 1) the lightbulb of 1 is also on.
Both switches are on (1 + 1), the lightbulb of 2 is on, and the 1 is off.
A 19th Century Adding Machine
We built an adding machine –a simple one– with the technology available before the 20th century. How cool is that? Relays were widely used in the telegraph infrastructure of that century, and George Boole introduced logical operations in 1847.
We used circuits with relays to build logical operations. Logical operations work with true and false. From the logical operations, we then built a machine that can work with numbers.
Those were the ideas that needed to be put together to have our calculator in the 19th century.
Electric circuits —> Logical operations
Logical operations —> Numerical operations
Conclusion
Computers nowadays use transistors instead of relays, hardware better suited for this task. Yet, the same principle applies; Computer Science starts at the Logic Gate level, the implementation of these is the domain of electrical engineers.
As long as the logic gates allow for a binary distinction, true/false, 1/0, on/off, we don’t care whether a logic gate uses water, electric current, or some alien technology. The system we developed using logic gates would work anyway.
We can say that Computer Science starts with the logic gates.