Logic gates

Milena Margaryan - Sep 23 - - Dev Community

Have you ever thought about what is happening behind the addition and subtraction of binary numbers? How do computers perform all these actions?
Hello everyone! In this blog post, I will briefly describe logic gates and how they work. Logic gates are electronic devices that function on one or more inputs to produce a single output. These devices are like building blocks that are used for not only simple operations but also complex computations.
The basic types of logic gates are the AND, OR, NOT, XOR, NOR, NAND, and XNOR gates. Let’s discuss all of them.

  1. The AND gate outputs 1 only in cases when all inputs are 1. The table of this function is shown below.
    0 0 0
    0 1 0
    1 0 0
    1 1 1

    1. The next gate is called an OR gate, which outputs 1 in the case when at least one of the inputs is also 1. 0 0 0 0 1 1 1 0 1 1 1 1
    2. The third logic gate is the NOT, which outputs the inverse of the input. 0 1 1 0
    3. The XOR gate outputs 1 in the case when exactly one input is equal to 1. 0 0 0 0 1 1 1 0 1 1 1 1
    4. The logic gate NOR is the opposite of the OR gate, so it outputs 1 only if both inputs are 0. 0 0 1 0 1 0 1 0 0 1 1 0
    5. Respectively the NAND gate is the opposite of the AND gate, so it outputs 0 only if both inputs are 1. 0 0 1 0 1 1 1 0 1 1 1 0
    6. And the last gate is the XNOR, which outputs 1 if both inputs are the same. 0 0 1 0 1 0 1 0 0 1 1 1

So another question is “How can we use these logic gates to calculate the sum and subtraction of binary numbers?”.
Let's start with the sum. For adding two binary numbers we should build a simple circuit for each vertical row. The first vertical row(which is on the right side) includes the sum of two binary digits and there is no carry(or the carry is equal to 0), so in this case we have two binary inputs and sum and carry outputs. These circuits are called half-adders.

Then as we already have a carry output we should find the sum of the next rows by using 3 inputs(two binary and one carry). The number of full adders in the circuit depends on the number of digits of binary numbers.

Now let’s find out which logic gates can be useful in calculating the sum of binary numbers. Looking at the tables of logic gates we will see that the logic gate XOR is similar to the sum of binary digits and the gate AND is similar to the carry. So our half adder will consist of these two gates and create an output from each of them. Then by inputting the carry and the sum of the previous row’s digits’ sum into another half adder, we will get a carry and a final sum for the row. The two carries should be included in the OR function to get the final carry for the next row. And then the process with the final adder will be repeated till we get the final result.

The subtraction is very similar to the addition, but in this case, instead of using half adders and full adders, we should use half subtractors and full subtractors. The half subtractor is the same as the half adder but in this case, the first digit is connected to the NOT gate before entering the AND gate. And again the whole story is being repeated as with the addition and the other difference between them is that instead of carrying we borrow here, which changes nothing in building the circuits. The understanding of this process demands much imagination, but I hope that everything was clear and that the post was very helpful for you)

.
Terabox Video Player