Boolean Expression Calculator
This calculator evaluates boolean (logical) expressions based on the truth values you assign to variables. It supports common logical operators like AND, OR, and NOT, along with parentheses for grouping.
Understanding Boolean Expressions
A boolean expression is a logical statement that evaluates to either True or False. These expressions are fundamental in computer science, digital electronics, and mathematical logic. They are used to make decisions, control program flow, and design digital circuits.
Key Components:
- Variables: Represent logical propositions that can be either True or False (e.g., A, B, C).
- Operators: Connect variables and sub-expressions to form more complex statements. The most common operators are:
- AND (Conjunction): Returns True only if ALL operands are True. (e.g., A AND B is True only if A is True AND B is True).
- OR (Disjunction): Returns True if AT LEAST ONE operand is True. (e.g., A OR B is True if A is True OR B is True OR both are True).
- NOT (Negation): Reverses the truth value of its operand. (e.g., NOT A is True if A is False, and False if A is True).
- Parentheses: Used to group parts of an expression, dictating the order of operations, similar to arithmetic. Operations inside parentheses are evaluated first.
How Boolean Expressions Work
When you evaluate a boolean expression, you substitute the truth values (True or False) of its variables into the expression and then apply the logical operators according to their precedence (NOT usually first, then AND, then OR, with parentheses overriding this order). The final outcome is a single True or False value.
Examples of Boolean Expressions:
A AND B: This expression is True only if both A and B are True.
A OR NOT B: This expression is True if A is True, or if B is False (or both).
(A AND B) OR C: This expression is True if both A and B are True, OR if C is True.
NOT (A OR B): This expression is True only if both A and B are False.
Using the Calculator:
- Enter your Boolean Expression: Type your logical statement into the "Boolean Expression" text area. Use the variables A, B, C, D, E and the operators AND, OR, NOT. Remember to use parentheses for complex groupings.
- Set Variable Truth Values: For each variable (A, B, C, D, E) that you use in your expression, select whether it is "True" or "False" using the dropdown menus.
- Calculate: Click the "Calculate Expression" button. The calculator will then display the final truth value (True or False) of your expression.
This tool is useful for verifying truth tables, understanding complex logical statements, or debugging conditions in programming and digital logic design.
.boolean-expression-calculator {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f9f9f9;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
max-width: 800px;
margin: 20px auto;
color: #333;
}
.boolean-expression-calculator h2, .boolean-expression-calculator h3 {
color: #0056b3;
border-bottom: 2px solid #eee;
padding-bottom: 10px;
margin-top: 20px;
}
.boolean-expression-calculator p {
line-height: 1.6;
margin-bottom: 10px;
}
.calculator-inputs .input-group {
margin-bottom: 15px;
}
.calculator-inputs label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.calculator-inputs textarea {
width: calc(100% – 20px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1em;
box-sizing: border-box;
}
.calculator-inputs small {
display: block;
margin-top: 5px;
color: #777;
font-size: 0.85em;
}
.variable-inputs {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin-bottom: 20px;
}
.input-group-inline {
display: flex;
align-items: center;
gap: 8px;
}
.input-group-inline label {
margin-bottom: 0;
font-weight: normal;
}
.calculator-inputs select {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1em;
background-color: #fff;
}
.calculator-inputs button {
background-color: #007bff;
color: white;
padding: 12px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1.1em;
transition: background-color 0.3s ease;
width: 100%;
box-sizing: border-box;
}
.calculator-inputs button:hover {
background-color: #0056b3;
}
.calculator-result {
background-color: #e9f7ef;
border: 1px solid #d4edda;
padding: 15px;
border-radius: 5px;
margin-top: 20px;
}
.calculator-result h3 {
color: #28a745;
margin-top: 0;
border-bottom: none;
padding-bottom: 0;
}
#booleanResult {
font-size: 1.4em;
font-weight: bold;
color: #28a745;
}
.calculator-article ul {
list-style-type: disc;
margin-left: 20px;
margin-bottom: 10px;
}
.calculator-article ol {
list-style-type: decimal;
margin-left: 20px;
margin-bottom: 10px;
}
.calculator-article li {
margin-bottom: 5px;
}
function calculateBooleanExpression() {
var expression = document.getElementById("expressionInput").value.trim();
var resultDiv = document.getElementById("booleanResult");
if (!expression) {
resultDiv.innerHTML = "Please enter a boolean expression.";
return;
}
// Get variable values
var A = document.getElementById("varA").value === "true";
var B = document.getElementById("varB").value === "true";
var C = document.getElementById("varC").value === "true";
var D = document.getElementById("varD").value === "true";
var E = document.getElementById("varE").value === "true";
// Prepare the expression for evaluation
var processedExpression = expression.toUpperCase(); // Case-insensitive operators
// Replace logical operators with JavaScript equivalents using word boundaries
processedExpression = processedExpression.replace(/\bAND\b/g, '&&');
processedExpression = processedExpression.replace(/\bOR\b/g, '||');
processedExpression = processedExpression.replace(/\bNOT\b/g, '!');
// Replace variable names with their current boolean values
// Using word boundaries to ensure 'A' doesn't replace part of 'ANOTHER'
processedExpression = processedExpression.replace(/\bA\b/g, A);
processedExpression = processedExpression.replace(/\bB\b/g, B);
processedExpression = processedExpression.replace(/\bC\b/g, C);
processedExpression = processedExpression.replace(/\bD\b/g, D);
processedExpression = processedExpression.replace(/\bE\b/g, E);
// Basic validation: check for unexpected characters after transformation
// This regex allows true, false, &&, ||, !, (, ), and whitespace
// It's a safeguard against injection, though not foolproof for all possible eval() attacks.
var allowedPattern = /^(true|false|&&|\|\||!|\(|\)|\s)*$/;
if (!allowedPattern.test(processedExpression)) {
// If the processed expression contains anything other than allowed boolean literals, operators, and parentheses, it's an error.
resultDiv.innerHTML = "Error: Invalid characters or unsupported syntax in expression. Please use only A-E, AND, OR, NOT, and parentheses.";
return;
}
try {
// Use eval() carefully. The replacements and validation above aim to ensure only boolean values and operators are present.
var finalResult = eval(processedExpression);
if (typeof finalResult !== 'boolean') {
resultDiv.innerHTML = "Error: Expression did not evaluate to a boolean value. Please check syntax.";
} else {
resultDiv.innerHTML = "
Result: " + (finalResult ? "True" : "False");
}
} catch (e) {
resultDiv.innerHTML = "Error evaluating expression: " + e.message + ". Please check syntax.";
}
}