2.11 Compound Boolean Expressions, Truth Tables & De Morgan's Optimization Laws
Executive Summary: Analyzing compound boolean logic and truth tables. We explore De Morgan's Laws for simplifying complex nested conditions, reducing branch instruction count in assembly, and ensuring logic inversion safety in mission-critical systems.
💻 1. Annotated Source Code
#include <iostream> using namespace std; int main() { bool isSunny = false; bool isWarm = false; cout << boolalpha; cout << "Is it sunny? " << isSunny << endl; cout << "Is it warm? " << isWarm << endl; return 0; }
📐 2. Architecture & UML Class Model
<<compilation-unit>>
WeatherSensorFusion
Sensor Evaluator
Attributes / Data Members
-isSunny : bool
-isWarm : bool
Operations / Methods
+evaluateOutdoorConditions(sunny: bool, warm: bool) : bool
+printRecommendation() : void
📚 3. Core C++ Concepts Deep-Dive
1. De Morgan's Laws
De Morgan's laws state that:
!(A && B) == (!A || !B)!(A || B) == (!A && !B)
Applying these rules simplifies boolean condition checks in firmware.
⚡ 4. Embedded Systems & Hardware Reality
1. Reducing Branch Instructions
Simplifying complex logical conditions reduces conditional branch instructions (BNE, BEQ), minimizing CPU pipeline hazard penalties.
💡 5. Production-Ready Embedded Refactoring
Simplified, branch-efficient flight safety check:
💡 Production-Ready Refactor
#include <cstdint> struct FlightConditions { bool is_battery_healthy; bool is_gps_locked; bool is_motor_armed; }; // Simplified using De Morgan's laws for fastest early exit constexpr bool isFlightReady(FlightConditions f) noexcept { return f.is_battery_healthy && f.is_gps_locked && f.is_motor_armed; }
📝 Knowledge Verification Quiz
Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.
Q1. According to De Morgan's Laws, what is the equivalent expression for '!(A && B)'?
Detailed Explanation:
De Morgan's law states that negating a logical AND produces a logical OR of the negated terms:
!(A && B) ≡ (!A || !B).
Q2. According to De Morgan's Laws, what is the equivalent expression for '!(A || B)'?
Detailed Explanation:
Negating a logical OR produces a logical AND of the negated terms:
!(A || B) ≡ (!A && !B).
Q3. Why does simplifying boolean expressions improve microcontroller assembly execution?
Detailed Explanation:
Simplified boolean expressions generate fewer conditional jumps in assembly, preventing branch mispredictions.
Q4. In the truth table for logical AND (A && B), how many out of the 4 input combinations yield 'true'?
Detailed Explanation:
Logical AND yields true only when both operands are true ($1 \times 1 = 1$).