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.
| ' + variables[j] + ' | '; } // Add expression header, replacing ' with superscript for better display tableHTML += '' + expression.replace(/'/g, '\'') + ' | '; tableHTML += '|
|---|---|---|
| ' + binaryString[l] + ' | '; // 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 += '' + (evaluatedResult ? '1' : '0') + ' | '; } catch (e) { tableHTML += 'Error | '; resultDiv.innerHTML = 'Error evaluating expression: ' + e.message + "; // Stop processing if an error occurs resultDiv.innerHTML += tableHTML + '
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:
- Enter your Boolean Expression: Type your expression into the input field.
- Use Supported Operators:
*for AND (e.g.,A * B)+for OR (e.g.,A + B)'for NOT (e.g.,A')- Use parentheses
()for grouping.
- Variables: Use single uppercase letters (A-Z) for your variables. The calculator will automatically detect them.
- Constants: You can use
0for False and1for True directly in your expression. - 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.