Big Idea Base 2 Math and Logic Gates
Software Development using Frontend and Backend Technologies
Popcorn Hack 1: Examples for Identifying Binary
Example 1:
Number: 101010
Yes – only contains 0s and 1s.
Example 2:
Number: 12301
No – contains digits other than 0 and 1
Example 3:
Number: 11001
Yes – only contains 0s and 1s.
Popcorn Hack 2: Examples for Adding and Subtracting Binary
Example 1: Adding Binary Numbers 101 + 110
101
-
110
1011 Number 11
Example 2: Subtracting Binary Numbers 1101 - 1011
1101
-
1011
0010 Number 10
Example 3: Adding Binary Numbers 111 + 1001
111
-
1001
1110 Number 14
Popcorn Hack 1
-
True or (False and False) True
-
(not True) and False False
-
True or (False and (not False)) True
Homework
Homework Hacks: Binary Converter
Task:
Your task is to write a program that converts between decimal and binary numbers.
Instructions:
- Create a function to convert a decimal number to binary.
- Create a function to convert a binary number (as a string) back to decimal.
- Test your functions with different numbers, both positive and negative.
```python
Function to convert decimal to binary
def decimal_to_binary(decimal): if decimal < 0: return ‘-‘ + bin(decimal)[3:] # Handles negative numbers return bin(decimal)[2:]
Function to convert binary to decimal
def binary_to_decimal(binary): return int(binary, 2)
Test with some numbers
decimal_number = 10 binary_number = “1010”
print(f”Decimal to Binary: {decimal_number} → {decimal_to_binary(decimal_number)}”) print(f”Binary to Decimal: {binary_number} → {binary_to_decimal(binary_number)}”)
Homework Hack 2: Difficulty Level Checker
```python import time
difficulty = input(“Enter difficulty (easy, medium, hard): “).lower().strip()
while difficulty != “easy” and difficulty != “medium” and difficulty != “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)