Binary Base 2 and Logic Gates HW and Popcorn Hacks
Popcorn Hack 1: Examples for Identifying Binary
Example 1:
- Number: 101010
- This is binary becuase there are only 0’s and 1’s
Example 2:
- Number: 12301
- Digits other than 0 and 1 are present
Example 3:
- Number: 11001
- This is binary becuase there are only 0’s and 1’s
Popcorn Hack 2: Adding and Subtracting Using Binary
Example 1:
- Binary Numbers: 101 + 110
- Add starting from the right: 1 + 0 = 1, 0 + 1 = 1, 1 + 1 = 10 (carry over 1). Result: 1011.
Example 2:
- Binary Numbers: 1101 - 1011
- Start from the right: 1 - 1 = 0, 0 - 1 requires borrowing, 10 - 0 = 10, 1 - 1 = 0. Result: 010.
Example 3:
- Binary Numbers: 111 + 1001
- Add starting from the right: 1 + 1 = 10 (carry over 1), 1 + 0 + 1 = 10 (carry over 1), 1 + 0 + 1 = 10 (carry over 1). Result: 1110.
Popcorn Hack 1:
What will be the result of this expression?
True or False and False
ANSWER: TRUE
Popcorn Hack 2:
What will be the result of this expression?
not True and False
ANSWER: FALSE
Popcorn Hack 3:
What will be the result of this expression?
True or False and not False
ANSWER: TRUE
Homework Hack: Binary Converter
Here is my code below:
def decimal_to_binary(decimal_num):
"""Converts a decimal number to binary (returns a string)."""
if decimal_num == 0:
return "0"
elif decimal_num < 0:
return "-" + bin(decimal_num)[3:] # Remove '-0b' prefix
else:
return bin(decimal_num)[2:] # Remove '0b' prefix
def binary_to_decimal(binary_str):
if binary_str.startswith("-"):
return -int(binary_str[1:], 2)
else:
return int(binary_str, 2)
# Testing
print("Decimal to Binary:")
print(f"10 → {decimal_to_binary(10)}")
print(f"-15 → {decimal_to_binary(-15)}")
print(f"0 → {decimal_to_binary(0)}")
print("\nBinary to Decimal:")
print(f"1010 → {binary_to_decimal('1010')}")
print(f"-1111 → {binary_to_decimal('-1111')}")
print(f"0 → {binary_to_decimal('0')}")
Decimal to Binary:
10 → 1010
-15 → -1111
0 → 0
Binary to Decimal:
1010 → 10
-1111 → -15
0 → 0
Homework Hack 2: Difficulty Level Checker
Here is my fixed code below:
import time
difficulty = input("Enter difficulty (easy, medium, hard): ").lower().strip()
while difficulty not in ["easy", "medium", "hard"]:
print("Please enter a valid difficulty level.")
difficulty = input("Enter difficulty (easy, medium, hard): ").lower().strip()
time.sleep(0.5)
print("Difficulty set to:", difficulty)
Please enter a valid difficulty level.
Please enter a valid difficulty level.
Difficulty set to: medium