๐ Logic Gates Overview#
Logic gates are the fundamental building blocks of digital circuits. They perform basic logical operations on one or more binary inputs to produce a single binary output.
Each gate follows a truth table that defines its behavior based on input combinations.
๐ง Types of Logic Gates#
1. AND Gate#
Symbol:
โ
Operation: Output is
1
only if both inputs are1
.Truth Table:
A
B
Output
0
0
0
0
1
0
1
0
0
1
1
1
2. OR Gate#
Symbol:
+
Operation: Output is
1
if at least one input is1
.Truth Table:
A
B
Output
0
0
0
0
1
1
1
0
1
1
1
1
3. NOT Gate#
Symbol:
ยฌ
or!
Operation: Inverts the input (
0
becomes1
,1
becomes0
).Truth Table:
A
Output
0
1
1
0
4. NAND Gate#
Operation: Opposite of AND. Output is
0
only if both inputs are1
.Truth Table:
A
B
Output
0
0
1
0
1
1
1
0
1
1
1
0
5. NOR Gate#
Operation: Opposite of OR. Output is
1
only if both inputs are0
.Truth Table:
A
B
Output
0
0
1
0
1
0
1
0
0
1
1
0
6. XOR Gate (Exclusive OR)#
Operation: Output is
1
if inputs are different.Truth Table:
A
B
Output
0
0
0
0
1
1
1
0
1
1
1
0
7. XNOR Gate (Exclusive NOR)#
Operation: Output is
1
if inputs are the same.Truth Table:
A
B
Output
0
0
1
0
1
0
1
0
0
1
1
1
๐ Summary Table#
Gate |
Symbol |
Operation Description |
Output is |
---|---|---|---|
AND |
|
Both inputs are |
A = 1 and B = 1 |
OR |
|
At least one input is |
A = 1 or B = 1 |
NOT |
|
Inverts the input |
A = 0 |
NAND |
|
Opposite of AND |
Not both A and B are |
NOR |
|
Opposite of OR |
Both A and B are |
XOR |
|
Inputs are different |
A โ B |
XNOR |
|
Inputs are the same |
A = B |
Logic gates are used to build combinational and sequential circuits, forming the backbone of processors, memory units, and digital systems.
import ipywidgets as widgets
from IPython.display import display, Markdown, clear_output
# ๐ Input Widgets
input_a = widgets.ToggleButton(value=False, description='Input A', button_style='info')
input_b = widgets.ToggleButton(value=False, description='Input B', button_style='info')
gate_selector = widgets.Dropdown(
options=['AND', 'OR', 'NOT', 'NAND', 'NOR', 'XOR', 'XNOR'],
value='AND',
description='๐ Select Gate:',
style={'description_width': 'initial'},
layout=widgets.Layout(width='300px')
)
output_area = widgets.Output()
### ๐ง Logic Gate Functions
gate_functions = {
'AND': lambda a, b: a and b,
'OR': lambda a, b: a or b,
'NOT': lambda a, _: not a,
'NAND': lambda a, b: not (a and b),
'NOR': lambda a, b: not (a or b),
'XOR': lambda a, b: a != b,
'XNOR': lambda a, b: a == b
}
### ๐ Truth Table Generator
def generate_truth_table(gate):
rows = []
for a in [0, 1]:
for b in [0, 1]:
result = gate_functions[gate](bool(a), bool(b)) if gate != 'NOT' else gate_functions[gate](bool(a), None)
rows.append(f"| {a} | {b if gate != 'NOT' else '-'} | {int(result)} |")
header = "| A | B | Output |\n|---|---|--------|"
return header + "\n" + "\n".join(rows)
### ๐ Update Display
def update_output(change=None):
output_area.clear_output()
a = input_a.value
b = input_b.value
gate = gate_selector.value
result = gate_functions[gate](a, b) if gate != 'NOT' else gate_functions[gate](a, None)
with output_area:
display(Markdown(f"### โ๏ธ Logic Gate: `{gate}`"))
display(Markdown(f"**Input A:** `{int(a)}` \n**Input B:** `{int(b) if gate != 'NOT' else 'N/A'}`"))
display(Markdown(f"**Output:** `{int(result)}`"))
display(Markdown("### ๐ Truth Table"))
display(Markdown(generate_truth_table(gate)))
### ๐ Observe Changes
input_a.observe(update_output, names='value')
input_b.observe(update_output, names='value')
gate_selector.observe(update_output, names='value')
### ๐ Display Interface
display(widgets.HBox([input_a, input_b]))
display(gate_selector)
display(output_area)
update_output()