Boolean Theorem Calculator

Boolean Theorem Calculator (Truth Table Generator)

Enter a boolean expression using variables A-Z, operators '*' (AND), '+' (OR), "' (NOT), and parentheses '()'. The calculator will generate its truth table.

function calculateBooleanTheorem() { var expression = document.getElementById('expressionInput').value.trim().toUpperCase(); var resultDiv = document.getElementById('result'); resultDiv.innerHTML = "; // Clear previous results if (!expression) { resultDiv.innerHTML = 'Please enter a boolean expression.'; return; } // Validate allowed characters: A-Z for variables, 0-1 for constants, space, *, +, ', (, ) var allowedCharsRegex = /^[A-Z01\s\*\+\'\(\)]+$/; if (!allowedCharsRegex.test(expression)) { resultDiv.innerHTML = 'Invalid characters detected. Please use A-Z for variables, 0-1 for constants, * for AND, + for OR, \' for NOT, and parentheses ().'; return; } // Extract unique variables (A-Z) var variables = []; for (var i = 0; i = 'A' && char !A constantExpression = constantExpression.replace(/\(([^)]+)\)'/g, '!($1)'); // (A+B)' -> !(A+B) constantExpression = constantExpression.replace(/\*/g, '&&'); constantExpression = constantExpression.replace(/\+/g, '||'); // Replace 0 with false and 1 with true for consistent boolean evaluation constantExpression = constantExpression.replace(/0/g, 'false'); constantExpression = constantExpression.replace(/1/g, 'true'); var constantResult = eval('(' + constantExpression + ')'); resultDiv.innerHTML = 'Expression evaluates to: ' + (constantResult ? '1 (True)' : '0 (False)') + ''; } catch (e) { resultDiv.innerHTML = 'Invalid expression or syntax error: ' + e.message + "; } return; } if (variables.length > 5) { // Limit to 5 variables for readability (2^5 = 32 rows) resultDiv.innerHTML = 'Too many variables. Please limit to 5 variables (A-E) for a manageable truth table.'; return; } var numRows = Math.pow(2, variables.length); var tableHTML = ''; // Add variable headers for (var j = 0; j < variables.length; j++) { tableHTML += ''; } // Add expression header, replacing ' with superscript for better display tableHTML += ''; tableHTML += ''; for (var k = 0; k < numRows; k++) { tableHTML += ''; var binaryString = k.toString(2).padStart(variables.length, '0'); var currentExpressionForEval = expression; // Map variables to their current boolean values (0 or 1) var varMap = {}; for (var l = 0; l < variables.length; l++) { varMap[variables[l]] = (binaryString[l] === '1'); // true or false tableHTML += ''; // Add variable value to table row } // Prepare expression for JavaScript eval() // 1. Handle NOT operator: A' -> !A, (A+B)' -> !(A+B) currentExpressionForEval = currentExpressionForEval.replace(/([A-Z])'/g, '!$1'); currentExpressionForEval = currentExpressionForEval.replace(/\(([^)]+)\)'/g, '!($1)'); // 2. Replace boolean operators with JavaScript equivalents currentExpressionForEval = currentExpressionForEval.replace(/\*/g, '&&'); currentExpressionForEval = currentExpressionForEval.replace(/\+/g, '||'); // 3. Substitute variables with 'true' or 'false' for (var varName in varMap) { if (varMap.hasOwnProperty(varName)) { // Use word boundary to ensure only whole variable names are replaced var regex = new RegExp('\\b' + varName + '\\b', 'g'); currentExpressionForEval = currentExpressionForEval.replace(regex, varMap[varName] ? 'true' : 'false'); } } // 4. Replace 0 with false and 1 with true for consistent boolean evaluation currentExpressionForEval = currentExpressionForEval.replace(/0/g, 'false'); currentExpressionForEval = currentExpressionForEval.replace(/1/g, 'true'); var evaluatedResult; try { // Wrap in parentheses to ensure the entire expression is evaluated as one unit evaluatedResult = eval('(' + currentExpressionForEval + ')'); tableHTML += ''; } catch (e) { tableHTML += ''; resultDiv.innerHTML = 'Error evaluating expression: ' + e.message + "; // Stop processing if an error occurs resultDiv.innerHTML += tableHTML + '
' + variables[j] + '' + expression.replace(/'/g, '\'') + '
' + binaryString[l] + '' + (evaluatedResult ? '1' : '0') + 'Error
'; return; } tableHTML += ''; } tableHTML += ''; resultDiv.innerHTML = tableHTML; } .calculator-container { font-family: Arial, sans-serif; background-color: #f9f9f9; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); max-width: 800px; margin: 20px auto; } .calculator-container h2 { color: #333; text-align: center; margin-bottom: 20px; } .calculator-container p { color: #555; line-height: 1.6; } .calc-input-group { margin-bottom: 15px; } .calc-input-group label { display: block; margin-bottom: 5px; font-weight: bold; color: #333; } .calc-input-group input[type="text"] { width: calc(100% – 22px); padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; } .calculator-container button { background-color: #007bff; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; display: block; width: 100%; margin-top: 10px; } .calculator-container button:hover { background-color: #0056b3; } .calc-result { margin-top: 20px; padding: 15px; background-color: #e9ecef; border-radius: 4px; border: 1px solid #dee2e6; overflow-x: auto; /* For wide tables */ } .calc-result p { margin: 0; color: #333; } .truth-table { width: 100%; border-collapse: collapse; margin-top: 15px; } .truth-table th, .truth-table td { border: 1px solid #ccc; padding: 8px; text-align: center; } .truth-table th { background-color: #f2f2f2; font-weight: bold; } .truth-table tbody tr:nth-child(even) { background-color: #f8f8f8; } .truth-table sup { vertical-align: super; font-size: 0.7em; }

Understanding Boolean Algebra and Truth Tables

Boolean algebra is a branch of algebra in which the values of the variables are the truth values true and false, usually denoted as 1 and 0 respectively. It is fundamental to digital electronics, computer science, and mathematical logic. Unlike elementary algebra where variables represent numbers, in Boolean algebra, variables represent propositions or states that can only be true or false.

Key Concepts and Operators:

  • Variables: Represented by letters (e.g., A, B, C), these can hold a value of either 0 (False) or 1 (True).
  • Constants: 0 (False) and 1 (True) can be used directly in expressions.
  • AND Operator (Conjunction): Denoted by '*' or '&'. The output is 1 only if all inputs are 1. (e.g., A * B)
  • OR Operator (Disjunction): Denoted by '+' or '|'. The output is 1 if at least one input is 1. (e.g., A + B)
  • NOT Operator (Negation): Denoted by "' or '~'. The output is the inverse of the input. (e.g., A')
  • Parentheses: Used to group operations and define precedence, just like in standard algebra.

Boolean Theorems:

Boolean algebra is governed by several theorems and postulates that allow for the simplification and manipulation of boolean expressions. Some common theorems include:

  • 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)
  • Identity Laws: A + 0 = A; A * 1 = A
  • Complement Laws: A + A' = 1; A * A' = 0
  • Idempotent Laws: A + A = A; A * A = A
  • De Morgan's Theorems: (A + B)' = A' * B'; (A * B)' = A' + B'

These theorems are crucial for simplifying complex logic circuits and optimizing computer algorithms.

What is a Truth Table?

A truth table is a mathematical table used in logic to compute the functional values of logical expressions on each of their functional arguments, that is, on each combination of values taken by their logical variables. It systematically lists all possible input combinations for a boolean expression and shows the resulting output for each combination. This calculator generates truth tables, allowing you to visualize the behavior of any boolean expression and verify boolean theorems.

How to Use This Calculator:

  1. Enter your Boolean Expression: Type your expression into the input field.
  2. Use Supported Operators:
    • * for AND (e.g., A * B)
    • + for OR (e.g., A + B)
    • ' for NOT (e.g., A')
    • Use parentheses () for grouping.
  3. Variables: Use single uppercase letters (A-Z) for your variables. The calculator will automatically detect them.
  4. Constants: You can use 0 for False and 1 for True directly in your expression.
  5. Click "Generate Truth Table": The calculator will display a table showing all possible truth value combinations for your variables and the corresponding output of your expression.

Example Expressions:

  • A + B (OR operation)
  • A * B (AND operation)
  • A' (NOT operation)
  • (A + B)' (De Morgan's Theorem example)
  • A * B + C' (A more complex expression)
  • (A + B) * (A' + C)
  • A + 0 (Example with a constant)

This tool is invaluable for students, engineers, and anyone working with digital logic or boolean algebra, providing a clear visual representation of logical functions.

Leave a Reply

Your email address will not be published. Required fields are marked *