🍿 Popcorn Hacks
Popcorn Hack 1: Binary to Decimal Converter
def binary_to_decimal(binary_str):
decimal = 0
for i in range(len(binary_str)):
decimal += int(binary_str[-(i + 1)]) * (2 ** i)
return decimal
# Get user input
binary_input = input("Enter a binary number: ")
decimal_output = binary_to_decimal(binary_input)
print(f"The decimal representation of {binary_input} is {decimal_output}.")
The decimal representation of 1010011 is 83.
Popcorn Hack 2: Binary Addition Battle
How it Works:
Step 1: The game randomly generates two binary numbers (between 0 and 255).
Step 2: The user must add these binary numbers and input the result.
Step 3: The game checks if the result is correct and measures how fast the user solved it, providing feedback and points based on performance.
import random
import time
def binary_addition_battle():
# Generate two random binary numbers (up to 8 bits)
num1 = bin(random.randint(0, 255))[2:]
num2 = bin(random.randint(0, 255))[2:]
# Show the binary numbers
print(f"Add the following binary numbers:")
print(f"Number 1: {num1}")
print(f"Number 2: {num2}")
# Start the timer
start_time = time.time()
# Ask the user for the sum
user_answer = input("Your answer (in binary): ")
# End the timer
end_time = time.time()
# Calculate the correct binary sum
correct_answer = bin(int(num1, 2) + int(num2, 2))[2:]
# Check if the answer is correct
if user_answer == correct_answer:
print(f"✅ Correct! You took {end_time - start_time:.2f} seconds.")
print(f"🏅 Your score: +10 points!")
else:
print(f"❌ Oops! The correct answer was {correct_answer}.")
print(f"🏅 Your score: -5 points.")
# Run the game
binary_addition_battle()
Add the following binary numbers:
Number 1: 111101
Number 2: 11110111
❌ Oops! The correct answer was 100110100.
🏅 Your score: -5 points.
Homework Hack
-
Explain in 1–2 sentences how to convert a binary number into a decimal number. You convert a binary number into a decimal by multiplying each bit by 2 raised to the power of its position (starting from the right at 0), then adding up all the results. For example, in 101, you calculate: 1×2² + 0×2¹ + 1×2⁰ = 4 + 0 + 1 = 5.
-
If you are given the binary number 11111111, what decimal number is that? The binary number 11111111 equals 255 in decimal. (Explanation: 1×2⁷ + 1×2⁶ + 1×2⁵ + 1×2⁴ + 1×2³ + 1×2² + 1×2¹ + 1×2⁰ = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255.)
Popcorn Hack 1
Question:
What are methods of real-world purpose that using logic gates can implement? Explain deeper if using our listed impacts, explaining why this impact is helpful.
Answer:
Logic gates are used in a variety of real-world systems. Here are some key examples and the associated impacts:
- Security Systems (Impact: Safety and Privacy)
- Logic gates control access systems like alarms and authentication processes. For example, an AND gate might be used to require two factors (e.g., a keycard and a pin) to trigger access.
- Why it’s helpful: Increases security by ensuring multiple conditions are met before granting access.
- Digital Circuits (Impact: Accuracy and Speed)
- Digital circuits that perform arithmetic and logic operations (e.g., in processors) rely on logic gates.
- Why it’s helpful: Logic gates ensure correct and fast computations, which are essential in modern computing systems.
- Traffic Light Control Systems (Impact: Efficiency and Safety)
- Logic gates help control traffic lights based on sensors and timers. AND, OR, and NOT gates are commonly used to coordinate safe traffic flow.
- Why it’s helpful: Increases road safety by ensuring traffic signals are managed logically and efficiently.
- Error Detection in Data Transmission (Impact: Reliability)
- XOR gates are used in error detection and correction algorithms, like parity checks.
- Why it’s helpful: Helps ensure data integrity during transmission, preventing data corruption.
Popcorn Hack 2
Question:
A digital circuit receives three binary inputs: X, Y, and Z. The circuit outputs 1 if and only if X AND Y are both 1, OR Z is 1. Which of the following expressions represents the circuit’s behavior?
A. (X AND Y) OR Z
B. X AND (Y OR Z)
C. (X OR Y) AND Z
D. NOT(X AND Y) OR Z
Answer:
The correct answer is A. (X AND Y) OR Z.
Explanation:
- The output is 1 if X AND Y are both 1, or if Z is 1.
- This directly matches the logic of the expression
(X AND Y) OR Z.
Homework Hack: Authorization System
Task:
Fill in the missing code necessary to implement a Python function that simulates a secure entry system using an AND gate.
def secure_entry_system(keycard, pin):
def AND(a, b):
return a & b # AND logic
return AND(keycard, pin)
# Test cases
print(secure_entry_system(1, 1)) # Expected Output: 1 (Access Granted)
print(secure_entry_system(0, 1)) # Expected Output: 0 (Access Denied)
# Answer
To add another variable like voice authorization, you can extend the function by incorporating another AND gate to check all three conditions (keycard, pin, and voice authorization):
```python
def secure_entry_system(keycard, pin, voice_authorization):
def AND(a, b):
return a & b # AND logic
# Combine keycard, pin, and voice authorization using AND gates
return AND(AND(keycard, pin), voice_authorization)
# Test cases
print(secure_entry_system(1, 1, 1)) # Expected Output: 1 (Access Granted)
print(secure_entry_system(0, 1, 1)) # Expected Output: 0 (Access Denied)
print(secure_entry_system(1, 0, 1)) # Expected Output: 0 (Access Denied)
print(secure_entry_system(1, 1, 0)) # Expected Output: 0 (Access Denied)
1
0
0
0