Understanding Boolean Theorems and Logic Expressions
Boolean algebra is a branch of algebra in which the values of the variables are the truth values true and false, usually denoted 1 and 0. It is the foundation of digital logic and computer science. Boolean theorems are fundamental rules that help simplify complex logical expressions and prove their equivalence.
Core Logic Operators:
AND (often represented as &, *, or ∧): True only if all operands are true.
OR (often represented as |, +, or ∨): True if at least one operand is true.
NOT (often represented as !, ~, or ¬): Inverts the truth value of an operand.
XOR (Exclusive OR, often represented as ^ or ⊕): True if exactly one of the operands is true.
Implies (often represented as -> or ⇒): If the first operand is true, the second must also be true for the implication to be true. If the first operand is false, the implication is always true.
Equivalence (often represented as , =, or ⇔): True if both operands have the same truth value.
Common Boolean Theorems:
These theorems are crucial for simplifying expressions:
Identity Laws: A + 0 = A, A * 1 = A
Null Laws: A + 1 = 1, A * 0 = 0
Idempotent Laws: A + A = A, A * A = A
Complement Laws: A + !A = 1, A * !A = 0
Commutative Laws: A + B = B + A, A * B = B * A
Associative Laws: (A + B) + C = A + (B + C), (A * B) * C = A * (B * C)
Distributive Laws: A * (B + C) = (A * B) + (A * C), A + (B * C) = (A + B) * (A + C)
Absorption Laws: A + (A * B) = A, A * (A + B) = A
De Morgan's Laws: !(A + B) = !A * !B, !(A * B) = !A + !B
Involutory Law: !!A = A
How This Calculator Works:
This calculator takes a symbolic Boolean expression as input. It identifies the unique variables (like 'A', 'B', 'C') present in the expression. For each variable, it iterates through all possible combinations of truth values (true/false). For each combination, it substitutes the values into the expression and evaluates it using standard Boolean logic rules. The result provided is the truth table, showing the output for every possible input combination.
Use Cases:
Digital Circuit Design: Simplifying complex logic gates.
Computer Science Algorithms: Analyzing and optimizing logical conditions.
Formal Verification: Proving the correctness of logical systems.
Educational Tool: Learning and experimenting with Boolean algebra concepts.
Note: This calculator currently supports basic AND, OR, NOT operators, parentheses for grouping, and variables represented by single uppercase letters (A-Z). More complex operators or functions would require extended parsing logic.
function evaluateBooleanExpression() {
var expressionInput = document.getElementById("expression").value.trim();
var resultValueElement = document.getElementById("resultValue");
if (!expressionInput) {
resultValueElement.innerHTML = "Please enter a boolean expression.";
return;
}
try {
// — Variable Extraction —
var variables = [];
for (var i = 0; i = 'A' && char <= 'Z') {
if (variables.indexOf(char) === -1) {
variables.push(char);
}
}
}
variables.sort(); // Consistent order
// — Truth Table Generation —
var numVars = variables.length;
var numRows = Math.pow(2, numVars);
var tableHTML = "
";
// Data rows
tableHTML += "";
for (var k = 0; k < numRows; k++) {
var assignment = {};
var rowValues = [];
for (var l = 0; l < numVars; l++) {
// Get the truth value for the current variable in this row
// (numVars – 1 – l) ensures the leftmost variable changes slowest
var value = (k % Math.pow(2, numVars – l)) < Math.pow(2, numVars – l – 1) ? 0 : 1;
assignment[variables[l]] = value;
rowValues.push(value);
tableHTML += "
" + (value === 1 ? "T" : "F") + "
";
}
var evaluatedResult = evaluateSingleRow(expressionInput, assignment);
tableHTML += "
" + (evaluatedResult === 1 ? "T" : "F") + "
";
tableHTML += "";
}
tableHTML += "
";
resultValueElement.innerHTML = tableHTML;
} catch (error) {
resultValueElement.innerHTML = "Error evaluating expression: " + error.message;
console.error(error);
}
}
function evaluateSingleRow(expression, assignment) {
var processedExpression = expression.toUpperCase();
// Replace variable names with their assigned truth values (0 or 1)
for (var variable in assignment) {
var regex = new RegExp(variable, 'g');
processedExpression = processedExpression.replace(regex, assignment[variable].toString());
}
// — Operator Mapping and Evaluation —
// Normalize operators to simple symbols for easier parsing
processedExpression = processedExpression.replace(/AND|&/g, '&&');
processedExpression = processedExpression.replace(/OR|\|/g, '||');
processedExpression = processedExpression.replace(/NOT|!/g, '!');
processedExpression = processedExpression.replace(/XOR|\^/g, '^'); // Bitwise XOR works for boolean XOR with 0/1
processedExpression = processedExpression.replace(/IMPLIES|->/g, ' B is equivalent to !A || B, which is equivalent to A <= B in JS for 0/1
processedExpression = processedExpression.replace(/EQUIV||=|/g, '=='); // Equality check for equivalence
// — Safe Evaluation using Function Constructor —
// This is a safer alternative to eval() for structured input
try {
// Attempt to directly evaluate using JavaScript's boolean logic
// Need to ensure it handles the JS operators correctly after mapping
// The mapping above is crucial here.
// A simple direct JS eval might work if mapping is perfect, but let's be more robust.
// The ' "1 || 1" -> 1 (True)
// Example: "A <= B" becomes "0 true (1)
// Example: "A == B" becomes "0 == 1" -> false (0)
// The function constructor approach isolates the evaluation scope.
var evaluator = new Function('return ' + processedExpression);
var result = evaluator();
// Ensure the result is strictly 0 or 1
if (typeof result === 'boolean') {
return result ? 1 : 0;
} else if (result === 0 || result === 1) {
return result;
} else {
// Handle cases where JS might return other numbers (e.g. from complex expressions not purely boolean)
// For strict boolean logic, anything non-zero is often treated as true, but we want 0 or 1.
// Let's assume the expression, after proper substitution and mapping, should yield a boolean.
// If it yields something else, it indicates an issue or unsupported construct.
throw new Error("Expression evaluated to a non-boolean value: " + result);
}
} catch (e) {
// If direct JS evaluation fails, it might be due to syntax errors or unsupported operators.
// We can attempt a more manual, step-by-step evaluation for common operators if needed,
// but the Function constructor with proper mapping is generally preferred for robustness.
throw new Error("Failed to evaluate: " + e.message);
}
}