3.6 Base 2 Math and Logic Gates
Notes and Hacks for 3.6 Base 2 Math and Logic Gates
Binary (Base-2) Basics
- Binary uses two digits: 0 and 1 (OFF/ON states).
- Bits: Each digit in binary.
- Why Binary? Reliable for storing/transmitting data — all data (text, images, sound) are just long binary sequences.
Comparing Number Systems
Number | Decimal (Base 10) | Binary (Base 2) | Octal (Base 8) | Hexadecimal (Base 16) |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
1 | 1 | 1 | 1 | 1 |
2 | 2 | 10 | 2 | 2 |
4 | 4 | 100 | 4 | 4 |
8 | 8 | 1000 | 10 | 8 |
15 | 15 | 1111 | 17 | F |
- Octal: Early computing.
- Hexadecimal: Memory addresses, color codes.
Binary Conversions
- Decimal → Binary: Divide by 2, record remainders bottom-up.
- Binary → Decimal: Multiply each bit by 2ⁿ based on position and sum.
Example:
13 → 1101
Binary Arithmetic
- Addition Rules:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 = 10 (carry 1)
- Subtraction Rules:
- 0 - 0 = 0
- 1 - 0 = 1
- 1 - 1 = 0
- 0 - 1 = 1 (borrow 1)
Two’s Complement (Negative Numbers)
- Invert all bits.
- Add 1.
- Example: 5 →
00000101
→ flip →11111010
→ add 1 →11111011
(−5)
- Example: 5 →
Bit Shifts
- Left Shift: Multiply by 2.
- Right Shift: Divide by 2.
Real-World Applications
- Storage: 1 Byte = 8 Bits, 1 KB = 1024 Bytes, etc.
- Networking: IP addresses in binary (e.g., 192.168.1.1).
Popcorn Hack 1: Identifying Binary
Example 1
Number: 101010
- ✅ Only contains 0s and 1s → This is a valid binary number
Example 2
Number: 12301
- ❌ Contains digits other than 0 and 1 → This is NOT a valid binary number
Example 3
Number: 11001
- ✅ Only contains 0s and 1s → This is a valid binary number
Popcorn Hack 2: Examples for Adding and Subtracting Binary
Example 1 (Adding):
Binary Numbers: 101 + 110
✅ Answer: 1011
Example 2 (Subtracting):
Binary Numbers: 1101 - 1011
✅ Answer: 010
Example 3 (Adding):
Binary Numbers: 111 + 1001
✅ Answer: 1110
Popcorn Hacks and Homework Hacks Completed
Popcorn Hack 1
Problem:
What is the result of:
True or False and False
Answer:
False and False
→False
True or False
→True
✅ Final Answer: True
Popcorn Hack 2
Problem:
What is the result of:
not True and False
Answer:
not True
→False
False and False
→False
✅ Final Answer: False
Popcorn Hack 3
Problem:
What is the result of:
True or False and not False
Answer:
not False
→True
False and True
→False
True or False
→True
✅ Final Answer: True
Homework Hack 1: Binary Converter
# Decimal to Binary
def decimal_to_binary(n):
if n == 0:
return "0"
binary = ""
while n > 0:
binary = str(n % 2) + binary
n = n // 2
return binary
# Binary to Decimal
def binary_to_decimal(b):
return int(b, 2)
# Example Tests
print("Decimal 10 to Binary:", decimal_to_binary(10)) # Output: 1010
print("Binary 1010 to Decimal:", binary_to_decimal("1010")) # Output: 10
Homework Hack 2: Difficulty Level Checker
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)