DEV Community

Milena Ghazaryan
Milena Ghazaryan

Posted on

Logic Gates

Inside your computer, there are billions of units called transistors, which fulfill several functions but commonly act as a very small light switch. Each switch can be turned on and turned off. In computer science, the turned-on switch is represented by the number 1 and the one turned off by 0. These zeros and ones form a number system called the binary, the computer's fundamental language. Due to this system, computers make calculations, create documents, view images, etc.
Via the function of switches, computers store information, and each switch stores zero or one. However, this doesn't mean that computers only store the data; they process it and transform inputs into outputs. And here, we start to use logic gates, which are the building blocks of computer circuits. Logic gates take an input and produce an output based on a specific logical operation. The most commonly used logic gates are NOT, AND, OR, NAND, NOR, XOR, and XNOR.

The NOT gate takes input and inverts it while representing the output. If it takes 0 as an input, its output is 1, and vice versa. Therefore, the output is the opposite, whatever the input is.
The AND gate is a logic gate which takes two inputs instead of one and gives a single output. The AND gate outputs 1 when both of our inputs are 1. In all other cases (A and B are inputs: A=0, B=1; A=1, B=0; A=0, B=0), it outputs 0 — meanwhile, the OR gate outputs 1 when A=1 or B=1. When both inputs are 0, the OR gate produces 0. However, if either of the gates is 1, it outputs 1 (also, when we have A=1 and B=1, the output is 1).
The NAND gate inverts the result of an AND calculation, so this is equivalent to the AND gate followed by the NOT gate. In other words, all the outputs are inverted: when A=1 and B=1, the output is 0; A=0 and B=0, the output is 1; A=1 and B=0, or A=0 and B=1, the output is 1. In the same way, the NOR gate exists, which is the opposite of OR. It inverses the outputs of the OR gate: when A=1 and B=1, the output is 0; A=0 and B=0, the output is 1; A=1 and B=0, or A=0 and B=1, the output is 0.
The exclusive or gate or XOR outputs one when exactly one of its inputs is 1. Consequently, when A=1 and B=0 or A=0 and B=1, the output is 1, otherwise, it outputs 0( when A=0 and B=0 or A=1 and B=1, the output is 0).There is also a gate to invert the XOR, the exclusive nor or XNOR gate. This gate does the opposite of what the XOR does. Thus, it outputs 1 when we have both inputs 0 or 1 ( A=1 and B=1, the output is 1; A=0 and B=0, the output is 1), and when either of the inputs is 0, the output is 0 (when A=1 and B=0 or A=0 and B=1, the output is 0).Through these logic gates, computers can perform various complex operations.

Here you can see the diagrams of logic gates and their truth tables.

Image description

And here is a funny reel about the logic gates. (Scan the QR to watch the reel)
Image description

Top comments (0)