Mathematics for Computing - Logical Operators and Boolean Algebra Lab Notes

Faculty of Computing - IT1130 Mathematics for Computing

  • Course Details: Year 11, Semester 11 (20262026)
  • Document Context: Lab Sheet 0303 focusing on Logic and Base Conversions

Relational Expressions and Logical Operators

  • Logical Operators Definition: The truth value of a Boolean expression is determined by combining the truth values of its component expressions using logical operators.
    • OR (||): This operator returns truetrue if at least one component expression is truetrue.
    • AND (&&): This operator returns truetrue only if all component expressions are truetrue.
    • NOT (\sim): This operator inverts the truth value; truetrue becomes falsefalse, and falsefalse becomes truetrue.

Section 1.1 - Basic Evaluation

  • Evaluation of relational assignments:
    • a=10>3a = 10 > 3: The condition is truetrue, so a=1a = 1.
    • b=5==7b = 5 == 7: The condition is falsefalse, so b=0b = 0.
    • c=8 =8c = 8 ~= 8: The condition "not equal to" is falsefalse, so c=0c = 0.

Section 1.2 - Logical Operations

  • Given Variables:

    • a=4<6a = 4 < 6 (Result: truetrue, value is 11)
    • b=10>=12b = 10 >= 12 (Result: falsefalse, value is 00)
  • Evaluation Tasks:

    • a \text{ && } b: Evaluates to 1 \text{ && } 0, resulting in 00 (falsefalse).
    • a || ba \text{ || } b: Evaluates to 1 || 01 \text{ || } 0, resulting in 11 (truetrue).
    • a\sim a: Evaluates to 1\sim 1, resulting in 00 (falsefalse).
    • \sim (a \text{ && } b): Evaluates to \sim (1 \text{ && } 0) = \sim (0), resulting in 11 (truetrue).

Operator Precedence Hierarchy

  • Order of Operations: Unless brackets are used to modify the order, logical operators are evaluated in the following sequence:

    1. NOT (\sim): Evaluated first.
    2. AND (&&): Evaluated second.
    3. OR (||): Evaluated last.
  • Evaluation Example 1:

    • Given: a=1,b=0,c=1a = 1, b = 0, c = 1
    • Expression: a \text{ || } b \text{ && } c
    • Step 1 (AND): b \text{ && } c = 0 \text{ && } 1 = 0
    • Step 2 (OR): a || 0=1 || 0=1a \text{ || } 0 = 1 \text{ || } 0 = 1
    • Result: 11
  • Evaluation Example 2:

    • Given: a=1,b=0,c=1a = 1, b = 0, c = 1
    • Expression: (a \text{ || } b) \text{ && } c
    • Step 1 (Brackets): a || b=1 || 0=1a \text{ || } b = 1 \text{ || } 0 = 1
    • Step 2 (AND): 1 \text{ && } c = 1 \text{ && } 1 = 1
    • Result: 11

Section 2.1 - Number Systems and Pattern Recognition

  • Task: Evaluate the function dec2bin(2n1)dec2bin(2^n - 1) for specific values of nn.
  • Evaluations:
    • For n=3n = 3: 231=72^3 - 1 = 7. Output: 111111
    • For n=4n = 4: 241=152^4 - 1 = 15. Output: 11111111
    • For n=5n = 5: 251=312^5 - 1 = 31. Output: 1111111111
    • For n=6n = 6: 261=632^6 - 1 = 63. Output: 111111111111
  • Pattern Identification: The binary output consists of a string of ones where the count of ones is exactly equal to the value of nn.

Section 3.1 - Basic Boolean Operations

  • Evaluation Q2:
    • 1 \text{ && } 0: Output is 00.
    • 1 || 01 \text{ || } 0: Output is 11.
    • 1\sim 1: Output is 00.

Section 3.2 - Boolean Expression Evaluation

  • Variables: A=1A = 1, B=0B = 0, C=1C = 1
  • Evaluations Q3:
    • (A \text{ && } B) \text{ || } C
      • Calculation: (1 \text{ && } 0) \text{ || } 1 = 0 \text{ || } 1 = 1
    • \sim (A \text{ || } B) \text{ && } C
      • Calculation: \sim (1 \text{ || } 0) \text{ && } 1 = \sim (1) \text{ && } 1 = 0 \text{ && } 1 = 0

Section 3.3 - Verifying Boolean Identities

  • Identity Check Q4: Evaluate if (A \text{ && } (A \text{ || } B)) == A
    • Trial with A=1,B=0A = 1, B = 0:
      • (1 \text{ && } (1 \text{ || } 0)) = (1 \text{ && } 1) = 1.
      • Since the result is 11, and A=1A = 1, the equation holds true (1==11 == 1).
    • Note: It is encouraged to test other combinations of values for AA and BB to verify this Absorption Law in Boolean Algebra.

Section 4.1 - Simple Decision Making Applications

  • Scenario Q5: Modeling a real-life eligibility check.
  • Given Data:
    • age=20age = 20
    • hasID=1hasID = 1 (representing truetrue)
  • Logic Model: (age >= 18) \text{ && } hasID
  • Step-by-step Evaluation:
    • (20>=18)(20 >= 18) evaluates to truetrue (11).
    • 1 \text{ && } 1 evaluates to 11.
  • Interpretation: The output 11 (truetrue) indicates that the condition for the decision (e.g., entry or legal eligibility) has been met.

Section 4.2 - Multiple Conditions Applications

  • Scenario Q6: Determining qualification based on academic performance and attendance.
  • Given Data:
    • marks=75marks = 75
    • attendance=80attendance = 80
  • Logic Model: (marks >= 50) \text{ && } (attendance >= 75)
  • Step-by-step Evaluation:
    • (75>=50)(75 >= 50) is truetrue (11).
    • (80>=75)(80 >= 75) is truetrue (11).
    • 1 \text{ && } 1 results in 11.
  • Testing Variability: Students should understand how altering values (e.g., setting marks=40marks = 40 or attendance=60attendance = 60) changes the outcome, demonstrating how multiple criteria are enforced simultaneously by the AND operator.