Program to Implement Logic Gates
Last Updated :
30 Aug, 2024
In a computer, most of the electronic circuits are made up logic gates. Logic gates are used to create a circuit that performs calculations, data storage or shows off object-oriented programming especially the power of inheritance. Logic gates can also be constructed using vacuum tubes, electromagnetic elements like optics, molecule etc.
What is Logic Gate?
A Logic gate is an elementary building block of any digital circuits. It takes one or two inputs and produces output based on those inputs. Outputs may be high (1) or low (0). Logic gates are implemented using diodes or transistors. There are seven basic logic gates defined, these are:
- AND gate,
- OR gate,
- NOT gate,
- NAND gate,
- NOR gate,
- XOR gate and
- XNOR gate.
Below are the brief details about them along with their implementation:
1. AND Gate
The AND gate gives an output of 1 if both the two inputs are 1, it gives 0 otherwise.
Below are the programs to implement AND gate using various methods:
- Using product method.
- Using if else condition.
- Using “AND (&&)” operator.
If-Else
// C program implementing the AND gate
// using if and else condition
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, ans;
for (i = 0; i < 5; i++) {
if (a[i] == 0 && b[i] == 0)
ans = 0;
else if (a[i] == 0 && b[i] == 1)
ans = 0;
else if (a[i] == 1 && b[i] == 0)
ans = 0;
else
ans = 1;
printf("\n %d AND %d = %d",
a[i], b[i], ans);
}
}
Product Method
// C program implementing the AND gate
// through product method.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, product;
for (i = 0; i < 5; i++) {
// using product method
product = a[i] * b[i];
printf("\n %d AND %d = %d",
a[i], b[i], product);
}
}
&amp; Operator
// C program implementing the AND gate
// using & operator
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, and_ans;
for (i = 0; i < 5; i++) {
// using the & operator
and_ans = a[i] & b[i];
printf("\n %d AND %d = %d",
a[i], b[i], and_ans);
}
}
Output 1 AND 0 = 0
0 AND 1 = 0
1 AND 1 = 1
0 AND 0 = 0
1 AND 0 = 0
2. OR Gate
The OR gate gives an output of 1 if either of the two inputs are 1, it gives 0 otherwise.
Below are the programs to implement AND gate using various methods:
- Using + operator.
- Using | operator.
- Using || operator.
- Using if else.
If-Else
// C program implementing the OR gate
// using if else
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, or_ans;
for (i = 0; i < 5; i++) {
// using the if-else conditions
if (a[i] == 0 && b[i] == 0)
or_ans = 0;
else
or_ans = 1;
printf("\n %d AND %d = %d",
a[i], b[i], or_ans);
}
}
+ Operator
// C program implementing the OR gate
// using + operator
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, or_ans;
for (i = 0; i < 5; i++) {
// using the + operator
if (a[i] + b[i] > 0)
or_ans = 1;
else
or_ans = 0;
printf("\n %d AND %d = %d",
a[i], b[i], or_ans);
}
}
| Operator
// C program implementing the OR gate
// using | operator
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, or_ans;
for (i = 0; i < 5; i++) {
// using the | operator
or_ans = a[i] | b[i];
printf("\n %d AND %d = %d",
a[i], b[i], or_ans);
}
}
|| operator
// C program implementing the OR gate
// using || operator
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, or_ans;
for (i = 0; i < 5; i++) {
// using the || operator
or_ans = a[i] || b[i];
printf("\n %d AND %d = %d",
a[i], b[i], or_ans);
}
}
Output 1 AND 0 = 1
0 AND 1 = 1
1 AND 1 = 1
0 AND 0 = 0
1 AND 0 = 1
3. NAND Gate
The NAND gate (negated AND) gives an output of 0 if both inputs are 1, it gives 1 otherwise.
Below are the programs to implement NAND gate using various methods:
- Using if else.
- Using Complement of the product.
If-Else
// C program implementing the NAND gate
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, ans;
for (i = 0; i < 5; i++) {
if (a[i] == 1 && b[i] == 1)
ans = 0;
else
ans = 1;
printf("\n %d NAND %d = %d",
a[i], b[i], ans);
}
}
Complement of the product
// C program implementing the NAND gate
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, ans;
for (i = 0; i < 5; i++) {
ans = !(a[i] * b[i]);
printf("\n %d NAND %d = %d",
a[i], b[i], ans);
}
}
Output 1 NAND 0 = 1
0 NAND 1 = 1
1 NAND 1 = 0
0 NAND 0 = 1
1 NAND 0 = 1
4. NOR Gate
The NOR gate (negated OR) gives an output of 1 if both inputs are 0, it gives 1 otherwise.
Below are the programs to implement NOR gate using various methods:
- Using + Operator.
- Using if else.
+ Operator.
// C program implementing the NOR gate
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, ans;
for (i = 0; i < 5; i++) {
ans = !(a[i] + b[i]);
printf("\n %d NOR %d = %d",
a[i], b[i], ans);
}
}
If-Else
// C program implementing the NOR gate
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, ans;
for (i = 0; i < 5; i++) {
if (a[i] == 0 && b[i] == 0)
ans = 1;
else
ans = 0;
printf("\n %d NOR %d = %d",
a[i], b[i], ans);
}
}
Output 1 NOR 0 = 0
0 NOR 1 = 0
1 NOR 1 = 0
0 NOR 0 = 1
1 NOR 0 = 0
5. NOT Gate
It acts as an inverter. It takes only one input. If the input is given as 1, it will invert the result as 0 and vice-versa.
Below are the programs to implement NOT gate using various methods:
- Using ! Operator.
- Using if else.
If-Else
// C program implementing the NOT gate
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int i, ans;
for (i = 0; i < 5; i++) {
if (a[i] == 0)
ans = 1;
else
ans = 0;
printf("\n NOT %d = %d", a[i], ans);
}
}
! Operator
// C program implementing the NOT gate
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = {1, 0, 1, 0, 1};
int i, ans;
for (i = 0; i < 5; i++)
{
ans = !(a[i]);
printf("\n NOT %d = %d", a[i], ans);
}
}
Output NOT 1 = 0
NOT 0 = 1
NOT 1 = 0
NOT 0 = 1
NOT 1 = 0
Conclusion
Logic gates are basic building components of digital circuits necessary to design complex systems. They are used to perform logical operations. It is important to understanding the working and combination of these gates in order to design circuits that perform tasks correctly and efficiently. Logic gates can help develop a solid foundation in digital electronics and pave the way for exploring more advanced topics.
Similar Reads
Two Level Implementation of Logic Gates
The term "two-level logic" refers to a logic design that uses no more than two logic gates between input and output. This does not mean that the entire design will only have two logic gates, but it does mean that the single path from input to output will only have two logic gates. In two-level logic
10 min read
C++ program to implement Half Adder
Prerequisite : Half Adder in Digital Logic We are given with two inputs A and B. Our task is to implement Half Adder circuit and print the outputs sum and carry of two inputs. Introduction : The Half adder is a combinational circuit which add two1-bit binary numbers which are augend and addend to gi
2 min read
Python program to implement Full Adder
Prerequisite : Full Adder in Digital LogicGiven three inputs of Full Adder A, B,C-IN. The task is to implement the Full Adder circuit and Print output i.e sum and C-Out of three inputs. Full Adder : A Full Adder is a logical circuit that performs an addition operation on three one-bit binary numbers
2 min read
Python program to implement 2:4 Multiplexer
Prerequisite : Multiplexers in Digital Logic Introduction : It is a combinational circuit which have many data inputs and single output depending on control or select inputs.â For N input lines, log n (base2) selection lines, or we can say that for 2n input lines, n selection lines are required. Mul
3 min read
C++ program to implement full subtractor
Prerequisite : Full SubtractorGiven three inputs of Full Subtractor A, B, Bin. The task is to implement the Full Subtractor circuit and Print output states difference (D) and Bout of three inputs. Introduction :The full subtractor is used to subtract three 1-bit numbers which are minuend, subtrahend
2 min read
Introduction of Logic Gates
In Boolean Algebra, there are three basic operations, [Tex] +,\:.\:,\:^\prime [/Tex] which are analogous to disjunction, conjunction, and negation in propositional logic . Each of these operations has a corresponding logic gate. Apart from these, there are a few other logic gates as well. It was inv
10 min read
Implementation of OR Gate from NOR Gate
The Logic gates are the Fundamental Building Blocks of the Digital Circuits. The Logic Gate takes one or more Binary inputs and performs logical Operations to Produce a single binary Output. Understanding through the Different Combinations of gates is important for the Designer to produce the desire
5 min read
Implementation of NOR gate using 2 : 1 Mux
In Digital Electronics, the concepts of Gates and Mux are a must to design the Digital circuit. The gates are the building block in Digital electronics and a MUX is a combinational logic circuit used in Digital Electronics. By Combining different types of gates we can design a complex circuit that c
6 min read
Python Program to implement Full Subtractor
Prerequisite -Full Subtractor in Digital Logic In this, we will discuss the overview of the full subtractor and will implement the full subtractor logic in the python language. Also, we will cover with the help of examples. Let's discuss it one by one. Given three inputs of Full Subtractor A, B, Bin
4 min read
Programming Array Logic
Programmable Array Logic (PAL) is a commonly used programmable logic device (PLD). It has programmable AND array and fixed OR array. Because only the AND array is programmable, it is easier to use but not flexible as compared to Programmable Logic Array (PLA). PAL's only limitation is number of AND
5 min read