Question
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.
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. It can also be constructed using vacuum tubes, electromagnetic elements like optics, molecules, etc. In a computer, most of the electronic circuits are made up of 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.
There are seven basic logic gates defined, these are AND gate, OR gate, NOT gate, NAND gate, NOR gate, XOR gate, and XNOR gate.
1. AND Gate
The AND gate gives an output of 1 if both the two inputs are 1, it gives 0 otherwise.
def AND (a, b):
if a == 1 and b == 1:
return True
else:
return False
Output:
1 2 Result 0 0 | 0 0 1 | 0 1 0 | 0 1 1 | 1
2. NAND Gate
The NAND gate (negated AND) gives an output of 0 if both inputs are 1, it gives 1 otherwise.
def NAND (a, b):
if a == 1 and b == 1:
return False
else:
return True
Output:
1 2 Result 0 0 | 1 0 1 | 1 1 0 | 1 1 1 | 0
3. OR Gate
The OR gate gives an output of 1 if either of the two inputs are 1, it gives 0 otherwise.
def OR (a, b):
if a == 1:
return True
elif b == 1:
return True
else:
return False
Output:
1 2 Result
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 1
4. XOR Gate
The XOR gate gives an output of 1 if either both inputs are different, it gives 0 if they are same.
def XOR (a, b):
if a != b:
return 1
else:
return 0
Output:
1 2 Result 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 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.
def NOT(a):
if(a == 0):
return 1
elif(a == 1):
return 0
Output:
1 Result 0 | 1 1 | 0
6. NOR Gate
The NOR gate (negated OR) gives an output of 1 if both inputs are 0, it gives 1 otherwise.
def NOR(a, b):
if(a == 0) and (b == 0):
return 1
elif(a == 0) and (b == 1):
return 0
elif(a == 1) and (b == 0):
return 0
elif(a == 1) and (b == 1):
return 0
Output:
1 2 Result 0 0 | 1 0 1 | 0 1 0 | 0 1 1 | 0
7. XNOR Gate
The XNOR gate (negated XOR) gives an output of 1 both inputs are same and 0 if both are different.
def XNOR(a,b):
if(a == b):
return 1
else:
return 0
Output:
1 2 Result 0 0 | 1 0 1 | 0 1 0 | 0 1 1 | 1
the code is
from itertools import product
def print_truth_table(op, n=2): prod = product([0, 1], repeat=n)
print(f'{op.__name__} Truth Table') print(*range(1, n+1),'Result') print('-------') for p in prod: print(*p, '|', int(op(*p))) print('------- ')
# 1 def AND (a, b): return 0
# 2 def NAND (a, b): return 0
# 3 def OR (a, b): return 0
# 4 def XOR (a, b): return 0 # 5 def NOT(a): return 0
# 6 def NOR(a, b): return 0
# 7 def XNOR(a,b): return 0
print(1, end='. ') print_truth_table(AND) print(2, end='. ') print_truth_table(NAND) print(3, end='. ') print_truth_table(OR) print(4, end='. ') print_truth_table(XOR) print(5, end='. ') print_truth_table(NOT, 1) print(6, end='. ') print_truth_table(NOR) print(7, end='. ') print_truth_table(XNOR)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started