๐Ÿ”Œ 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 are 1.

  • 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 is 1.

  • 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 becomes 1, 1 becomes 0).

  • Truth Table:

    A

    Output

    0

    1

    1

    0


4. NAND Gate#

  • Operation: Opposite of AND. Output is 0 only if both inputs are 1.

  • 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 are 0.

  • 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 1 Whenโ€ฆ

AND

โ‹…

Both inputs are 1

A = 1 and B = 1

OR

+

At least one input is 1

A = 1 or B = 1

NOT

ยฌ

Inverts the input

A = 0

NAND

ยฌ(Aโ‹…B)

Opposite of AND

Not both A and B are 1

NOR

ยฌ(A+B)

Opposite of OR

Both A and B are 0

XOR

โŠ•

Inputs are different

A โ‰  B

XNOR

ยฌ(AโŠ•B)

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()