Logic Gates
Homework Hacks and Popcorn Hacks
Popcorn Hack 1
AND Gate
- Security System: A building door unlocks only if a keycard is scanned AND a fingerprint matches.
- Both conditions must be true at the same time (Keycard present AND fingerprint match) for the output (unlock) to happen.
OR Gate
- Emergency Exit Alarm: If door A is opened OR door B is opened, the alarm will sound.
- Only one condition needs to be true to trigger the alarm (either door A or door B).
NOT Gate (Inverter)
- Automatic Night Lights: A sensor turns the lights on when it detects NOT daylight (i.e., when it’s dark).
- It flips the input: if the sensor detects no light (input = 0), the NOT gate switches it to output = 1 (turn light ON).
NAND Gate
- Microwave Oven Safety: The microwave won’t start unless door is closed AND start button pressed — if not both, it blocks operation (safety fail-safe).
- Prevents operation unless both conditions are met exactly; otherwise, it stops.
NOR Gate
- Motion Sensors at Night: If no motion detected and no light detected, it triggers night mode (e.g., locks doors, sets alarms).
- Only when both sensors detect nothing will it trigger night mode (both inputs 0 → output 1).
XOR Gate (Exclusive OR)
- Two-Button Start Machines: In some factory machines, you need to press only one button (not both) to start — Button A XOR Button B.
- If exactly one input is pressed, the machine starts. Pressing none or both = no start.
XNOR Gate (Exclusive NOR)
- Password Checker: A system where login access is granted only if both typed password and saved password match exactly.
- If both inputs are the same (both 1s or both 0s), access is allowed (XNOR checks for equality).
Popcorn Hack 2
A. (X AND Y) OR Z because X and Y both have to be 1 OR z has to be 1.
Homework Hack
def secure_entry_system(keycard, pin, voice_auth):
def AND(a, b):
return a & b # AND logic
return AND(AND(keycard, pin), voice_auth)
# Test cases
print(secure_entry_system(1, 1, 1))
print(secure_entry_system(1, 1, 0))
print(secure_entry_system(0, 1, 1))
print(secure_entry_system(1, 0, 1))
1
0
0
0