SanPranav / QcommVNE_Frontend

This repository contains our Trimester 3 Computer Science Principles work, featuring: Pyre Smart AI Based Fire Predictions
https://sanpranav.github.io/QcommVNE_Frontend/
Apache License 2.0
2 stars 0 forks source link

Binary Base 2 Math + Logic Gates #24

Open SanPranav opened 1 month ago

SanPranav commented 1 month ago

Binary Base 2 Math & Logic Gates

Overview

Binary is the foundation of all computing systems. Understanding binary (base 2) math and logic gates is essential for:


What is Binary?

Binary is a base-2 number system that uses only two digits: 0 and 1. Each position represents a power of 2, compared to decimal (base 10) where each position represents a power of 10.

Binary to Decimal Conversion

Binary | Calculation | Decimal -- | -- | -- 1 | 2⁰ = 1 | 1 10 | 2¹ + 0×2⁰ = 2 | 2 11 | 2¹ + 2⁰ = 3 | 3 100 | 2² + 0×2¹ + 0×2⁰ = 4 | 4 101 | 2² + 0×2¹ + 2⁰ = 5 | 5 1010 | 2³ + 0×2² + 2¹ + 0×2⁰ = 10 | 10

Logic Gate Implementation

NOT Gate (Inverter)

def NOT(A):
    return 1 if A == 0 else 0

Inverts the input signal.

If the input is 0, it outputs 1. If the input is 1, it outputs 0.


AND Gate

def AND(A, B):
    return 1 if A == 1 and B == 1 else 0

Output is 1 only when both inputs are 1.

This is like multiplication in binary: A × B.


OR Gate

def OR(A, B):
    return 1 if A == 1 or B == 1 else 0

Output is 1 when at least one input is 1.

Acts as a decision maker when any condition is true.


XOR Gate (Exclusive OR)

def XOR(A, B):
    return 1 if A != B else 0

Output is 1 when inputs are different.

XOR is particularly useful for binary addition without carries.


Building Complex Circuits

Half Adder

A half adder adds two binary digits, producing a sum and a carry.

def half_adder(A, B):
    sum_bit = XOR(A, B)
    carry = AND(A, B)
    return sum_bit, carry

Full Adder

A full adder adds three binary digits (two inputs plus a carry-in), producing a sum and a carry-out.

def full_adder(A, B, Cin):
    sum1, carry1 = half_adder(A, B)
    sum_bit, carry2 = half_adder(sum1, Cin)
    carry_out = OR(carry1, carry2)
    return sum_bit, carry_out

Classwork Problems (Easy)

Convert binary 1101 to decimal.

1101 = 1×2³ + 1×2² + 0×2¹ + 1×2⁰
     = 8 + 4 + 0 + 1
     = 13

Answer: 13


Add binary numbers 101 and 011.

  1 0 1
+ 0 1 1
------
  1 0 0 0

Answer: 1000 (which is 8 in decimal)


What is the output of an AND gate with inputs A=1 and B=0?

AND(1, 0) = 0

Answer: 0 – The AND gate outputs 1 only when both inputs are 1.


Homework Problems (Harder)

Convert decimal 42 to binary.

42 ÷ 2 = 21 remainder 0
21 ÷ 2 = 10 remainder 1
10 ÷ 2 = 5  remainder 0
5  ÷ 2 = 2  remainder 1
2  ÷ 2 = 1  remainder 0
1  ÷ 2 = 0  remainder 1

Reading remainders bottom-up: 101010 Answer: 101010


Design a circuit using basic gates that outputs 1 only when exactly two of its three inputs are 1.

(A∧B∧¬C) ∨ (A∧¬B∧C) ∨ (¬A∧B∧C)

Answer: This requires using AND, OR, and NOT gates to check each possible case where exactly two inputs are 1.


Implement an XOR gate using only AND, OR, and NOT gates.

XOR(A, B) = (A ∨ B) ∧ ¬(A ∧ B)

Answer: XOR is true when either A OR B is true, but not when both are true simultaneously.


Why Does Binary and Logic Matter?


Resources for Further Learning

📖 Khan Academy: Binary Number System 📖 Nand Game: Build a Computer from First Principles 📖 Computerphile: Binary Explained 📖 Logic.ly: Interactive Logic Gate Simulator 📖 MIT OpenCourseWare: Digital Circuits

# **Binary Base 2 Math & Logic Gates** ## **Overview** Binary is the foundation of all computing systems. Understanding **binary (base 2) math** and **logic gates** is essential for: - How computers store and process data - Digital circuit design - Low-level programming - Understanding computer architectures --- ## **What is Binary?** Binary is a base-2 number system that uses only two digits: **0** and **1**. Each position represents a power of 2, compared to decimal (base 10) where each position represents a power of 10. ### **Binary to Decimal Conversion** | Binary | Calculation | Decimal | |--------|-------------|---------| | 1 | 2⁰ = 1 | 1 | | 10 | 2¹ + 0×2⁰ = 2 | 2 | | 11 | 2¹ + 2⁰ = 3 | 3 | | 100 | 2² + 0×2¹ + 0×2⁰ = 4 | 4 | | 101 | 2² + 0×2¹ + 2⁰ = 5 | 5 | | 1010 | 2³ + 0×2² + 2¹ + 0×2⁰ = 10 | 10 | --- ## **Binary Operations** ### **Binary Addition** ``` 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10 (carry the 1) ``` **Example:** ``` 1 1 1 (carries) 1 0 1 + 0 1 1 ------- 1 0 0 0 ``` ### **Binary Subtraction** ``` 0 - 0 = 0 1 - 0 = 1 1 - 1 = 0 0 - 1 = 1 (borrow 1) ``` ### **Binary Multiplication** ``` 0 × 0 = 0 0 × 1 = 0 1 × 0 = 0 1 × 1 = 1 ``` --- ## **Logic Gates** Logic gates are the basic building blocks of digital circuits that implement Boolean logic operations. ### **Basic Logic Gates** | Gate | Symbol | Operation | Truth Table | |------|--------|-----------|------------| | NOT | ¬A | Inverts input | 0→1, 1→0 | | AND | A∧B | True only if both inputs are true | 0∧0=0, 0∧1=0, 1∧0=0, 1∧1=1 | | OR | A∨B | True if at least one input is true | 0∨0=0, 0∨1=1, 1∨0=1, 1∨1=1 | | XOR | A⊕B | True if inputs are different | 0⊕0=0, 0⊕1=1, 1⊕0=1, 1⊕1=0 | | NAND | ¬(A∧B) | Negation of AND | 0∧0=1, 0∧1=1, 1∧0=1, 1∧1=0 | | NOR | ¬(A∨B) | Negation of OR | 0∨0=1, 0∨1=0, 1∨0=0, 1∨1=0 | --- ## **Logic Gate Implementation** ### **NOT Gate (Inverter)** ```python def NOT(A): return 1 if A == 0 else 0 ``` **Inverts the input signal.** If the input is 0, it outputs 1. If the input is 1, it outputs 0. --- ### **AND Gate** ```python def AND(A, B): return 1 if A == 1 and B == 1 else 0 ``` **Output is 1 only when both inputs are 1.** This is like multiplication in binary: A × B. --- ### **OR Gate** ```python def OR(A, B): return 1 if A == 1 or B == 1 else 0 ``` **Output is 1 when at least one input is 1.** Acts as a decision maker when any condition is true. --- ### **XOR Gate (Exclusive OR)** ```python def XOR(A, B): return 1 if A != B else 0 ``` **Output is 1 when inputs are different.** XOR is particularly useful for binary addition without carries. --- ## **Building Complex Circuits** ### **Half Adder** A half adder adds two binary digits, producing a sum and a carry. ```python def half_adder(A, B): sum_bit = XOR(A, B) carry = AND(A, B) return sum_bit, carry ``` ### **Full Adder** A full adder adds three binary digits (two inputs plus a carry-in), producing a sum and a carry-out. ```python def full_adder(A, B, Cin): sum1, carry1 = half_adder(A, B) sum_bit, carry2 = half_adder(sum1, Cin) carry_out = OR(carry1, carry2) return sum_bit, carry_out ``` --- ## **Classwork Problems (Easy)** **Convert binary 1101 to decimal.** ``` 1101 = 1×2³ + 1×2² + 0×2¹ + 1×2⁰ = 8 + 4 + 0 + 1 = 13 ``` **Answer:** 13 --- **Add binary numbers 101 and 011.** ``` 1 0 1 + 0 1 1 ------ 1 0 0 0 ``` **Answer:** 1000 (which is 8 in decimal) --- **What is the output of an AND gate with inputs A=1 and B=0?** ``` AND(1, 0) = 0 ``` **Answer:** 0 – The AND gate outputs 1 only when both inputs are 1. --- ## **Homework Problems (Harder)** **Convert decimal 42 to binary.** ``` 42 ÷ 2 = 21 remainder 0 21 ÷ 2 = 10 remainder 1 10 ÷ 2 = 5 remainder 0 5 ÷ 2 = 2 remainder 1 2 ÷ 2 = 1 remainder 0 1 ÷ 2 = 0 remainder 1 ``` Reading remainders bottom-up: 101010 **Answer:** 101010 --- **Design a circuit using basic gates that outputs 1 only when exactly two of its three inputs are 1.** ``` (A∧B∧¬C) ∨ (A∧¬B∧C) ∨ (¬A∧B∧C) ``` **Answer:** This requires using AND, OR, and NOT gates to check each possible case where exactly two inputs are 1. --- **Implement an XOR gate using only AND, OR, and NOT gates.** ``` XOR(A, B) = (A ∨ B) ∧ ¬(A ∧ B) ``` **Answer:** XOR is true when either A OR B is true, but not when both are true simultaneously. --- ## **Why Does Binary and Logic Matter?** - **Foundation of all digital computing** - **Understanding how computers process information at the lowest level** - **Critical for hardware design and embedded systems** - **Enables efficient algorithm implementation** --- ## **Resources for Further Learning** 📖 [[Khan Academy: Binary Number System](https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/binary-search)](https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/binary-search) 📖 [[Nand Game: Build a Computer from First Principles](https://nandgame.com/)](https://nandgame.com/) 📖 [[Computerphile: Binary Explained](https://www.youtube.com/watch?v=LpuPe81bc2w)](https://www.youtube.com/watch?v=LpuPe81bc2w) 📖 [[Logic.ly: Interactive Logic Gate Simulator](https://logic.ly/)](https://logic.ly/) 📖 [[MIT OpenCourseWare: Digital Circuits](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-004-computation-structures-spring-2017/)](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-004-computation-structures-spring-2017/)