# Learn Logic Gates with C and Python Codes

## A logic gate is an electronic circuit that implements a Boolean function, i.e., it performs a logical operation on one or more binary inputs and produces a single binary output.

### Logic Gates

Logic Gates are the fundamental building blocks of digital electronics and digital circuits. These gates are the electronic circuits that perform logical operations on one or more input signals and provide a single output signal. The output signal is a result of the logical operation performed on the input signals. They allow for the creation of complex circuits by performing simple binary operations.

Logic gates are used in various areas of digital electronics and computer engineering, including:Digital Circuit Design: Logic gates are used to build complex digital circuits like computers, microcontrollers, etc. 1. Data Processing: Gates can be used to process data and make decisions based on inputs.

2. Memory Devices: Gates can be used to build memory devices such as flip-flops, which store binary information.

3. Sequential Circuits: Gates are used to build sequential circuits like timers, counters, and state machines.

4. Signal Processing: Gates can be used for signal processing applications, like signal amplification and filtering.

5. Control Systems: Gates can be used to control the flow of data in control systems, like in embedded systems.

6. Communication Systems: Gates can be used in communication systems, such as modems and multiplexers.

There are seven basic types of logic gates: AND, OR, NOT, NAND, NOR, XOR and XNOR.

1. AND Gate: The AND gate takes two or more inputs and provides an output of 1 if all inputs are 1 and an output of 0 if any input is 0.
1. OR Gate: The OR gate takes two or more inputs and provides an output of 1 if any input is 1 and an output of 0 if all inputs are 0.
1. NOT Gate: The NOT gate takes one input and provides the opposite output. If the input is 1, the output is 0, and if the input is 0, the output is 1.
1. NAND Gate: The NAND gate is a combination of the AND and NOT gates. The output of a NAND gate is 0 if all inputs are 1 and 1 if any input is 0.
1. NOR Gate: The NOR gate is a combination of the OR and NOT gates. The output of a NOR gate is 1 if all inputs are 0 and 0 if any input is 1. T
1. XOR Gate: The XOR gate is also known as the exclusive OR gate. The output of an XOR gate is 1 if either input is 1 but not both, and 0 if both inputs are the same.
1. XNOR Gate: The XNOR gate is a combination of the XOR and NOT gates. The output of an XNOR gate is 1 if both inputs are the same, and 0 if either input is different.
The image below shows the truth-tables for logic gates –

Logic gates are used to perform a variety of operations in digital electronics, such as adding, subtracting, multiplying, and dividing. They are also used in computer systems, communication systems, and control systems.

To get started with coding, it is important to understand the basics of logic gates and their applications. (Note: If you wish to experiment practically with logic gates and see how they actually work then you can buy the “Digital ICT Kit” for school/college/university students – available at robotechbd.com. This specially designed kit can be extremely useful for HSC students in Bangladesh!)

### Coding Truth-tables of Logic Gates Using C and Python Programming Languages

There are multiple ways to display the truth-tables of logic gates. In this section, we will use C and Python programming language as these two are the most used programming languages by students or beginners. Here are some examples of coding the basic logic gates (AND, OR, NOT, NAND, NOR, XOR) in the C and python programming languages:

1. Code for Truth-table of AND Gate in C
```  ```
#include <stdio.h>

int main()
{
printf("Truth Table for AND Gate:\n");
printf("A\tB\tResult\n");
printf("-------------------\n");

for (int a = 0; a <= 1; a++)
{
for (int b = 0; b <= 1; b++)
{
int result = a & b;
printf("%d\t%d\t%d\n", a, b, result);
}
}

return 0;
}
```
```

Code for Truth-table of AND Gate in Python

```  ```
def and_gate_truth_table():
print("AND gate truth table:")
print("A   B   | Output")
print("--------|-------")
for a in range(2):
for b in range(2):
output = a and b
print(f"{a}   {b}   |   {output}")

and_gate_truth_table()
```
```

2. Code for Truth-table of OR Gate in C

```  ```
#include <stdio.h>

int main() {
int a, b;
printf("A\tB\tA OR B\n");
for (a = 0; a <= 1; a++) {
for (b = 0; b <= 1; b++) {
printf("%d\t%d\t%d\n", a, b, a || b);
}
}
return 0;
}  ```
```

Code for Truth-table of OR Gate in Python

```  ```
def or_gate_truth_table():
print("OR gate truth table:")
print("A   B   | Output")
print("--------|-------")
for a in range(2):
for b in range(2):
output = a or b
print(f"{a}   {b}   |   {output}")

or_gate_truth_table()
```
```

3. Code for Truth-table of NOT Gate in C

```  ```
#include <stdio.h>

int main()
{
int input, output;

printf("NOT GATE TRUTH TABLE\n");
printf("INPUT\tOUTPUT\n");

input = 0;
output = !input;
printf("%d\t%d\n", input, output);

input = 1;
output = !input;
printf("%d\t%d\n", input, output);

return 0;
} ```
```

Code for Truth-table of NOT Gate in Python

```  ```
def not_gate_truth_table():
print("NOT gate truth table:")
print("A   | Output")
print("----|-------")
for a in range(2):
output = not a
print(f"{a}   |   {output}")

not_gate_truth_table()
```
```

4. Code for Truth-table of NAND gate in C

```  ```
#include <stdio.h>

int main() {
int a, b;

printf("NAND Truth Table\n");
printf("A\tB\tResult\n");

for (a = 0; a <= 1; a++) {
for (b = 0; b <= 1; b++) {
printf("%d\t%d\t%d\n", a, b, !(a & b));}
}
return 0;
} ```
```

Code for Truth-table of NAND Gate in Python

```  ```
def nand_gate_truth_table():
print("NAND gate truth table:")
print("A   B   | Output")
print("--------|-------")
for a in range(2):
for b in range(2):
output = not (a and b)
print(f"{a}   {b}   |   {output}")

nand_gate_truth_table()
```
```

“Learning must be fun and enjoyable! Only then you’ll be able to do great things with what you learn.”

Khaled Hussain, Chairman & CEO, Cybernetics Robo Ltd.

5. Code for Truth-table of NOR gate in C

```  ```
#include <stdio.h>

int main() {
int a, b;
printf("a\tb\tNOR\n");
printf("-----------------\n");
for (a = 0; a <= 1; a++) {
for (b = 0; b <= 1; b++) {
printf("%d\t%d\t%d\n", a, b, !(a || b));
}
}
return 0;
}```
```

Code for Truth-table of NOR Gate in Python

```  ```
def nor_gate_truth_table():
print("NOR gate truth table:")
print("A   B   | Output")
print("--------|-------")
for a in range(2):
for b in range(2):
output = not (a or b)
print(f"{a}   {b}   |   {output}")

nor_gate_truth_table()
}```
```

6. Code for Truth-table of XOR gate in C

```  ```
#include <stdio.h>

int main()
{
int a, b;

printf("a\tb\tXOR\n");
for (a = 0; a <= 1; a++)
{
for (b = 0; b <= 1; b++)
{
printf("%d\t%d\t%d\n", a, b, (a ^ b));
}
}

return 0;
}```
```

Code for Truth-table of XOR Gate in Python

```  ```
def xor_gate_truth_table():
print("XOR gate truth table:")
print("A   B   | Output")
print("--------|-------")
for a in range(2):
for b in range(2):
output = a ^ b
print(f"{a}   {b}   |   {output}")

xor_gate_truth_table()
```
```

7. Code for Truth-table of XNOR gate in C

```  ```
#include <stdio.h>

int main()
{
int a, b;

printf("Truth Table for XNOR Gate\n");
printf("A\tB\tResult\n");
for (a = 0; a <= 1; a++)
{
for (b = 0; b <= 1; b++)
{
printf("%d\t%d\t%d\n", a, b, !(a ^ b));
}
}
return 0;
} ```
```

Code for Truth-table of XNOR Gate in Python

```  ```
def xnor_gate_truth_table():
print("XNOR gate truth table:")
print("A   B   | Output")
print("--------|-------")
for a in range(2):
for b in range(2):
output = not (a ^ b)
print(f"{a}   {b}   |   {output}")

xnor_gate_truth_table()
```
```

In this article, we have learnt about logic gates and their truth-table implementation in C and Python programming languages.

In conclusion, logic gates are the fundamental building blocks of digital electronics. They allow for the creation of complex circuits by performing simple binary operations. The AND, OR, NOT, NAND, NOR, XOR, and XNOR gates are the seven basic types of logic gates. Understanding these gates and how they function is crucial for anyone looking to pursue a career in electronics and computer engineering. By implementing the truth tables for these gates, one can demonstrate their behavior and use them to design a variety of circuits. It’s important to note that these gates are just the beginning and more advanced gates like flip-flops and counters can be built using the basic logic gates. The field of digital electronics is constantly evolving and new technologies are being developed, but the basic principles of logic gates remain the same.

If you are interested to more about logic gates and electronics circuits, then we at Cybernetics Robo Academy are here to help you. We have specially designed courses like HSC ICT, PCB Design, Electronics, 3D Modelling and Printing and many more.  We also provide scholarship opportunities for suitable candidates. For a quick start, all you need is to grab your mobile phone and give us a call on 01761500020. Alternatively, you can inbox us via our Facebook page. Click to view our current courses.     