Supported: AND (&, .), OR (|, +), NOT (!, ~), XOR (^), Parentheses ()
1 (True)
0 (False)
1 (True)
0 (False)
1 (True)
0 (False)
Resulting Truth Value
function calculateBoolean() {
var expression = document.getElementById('booleanExpression').value.trim();
var valA = document.getElementById('varA').value === "1";
var valB = document.getElementById('varB').value === "1";
var valC = document.getElementById('varC').value === "1";
var resultDiv = document.getElementById('booleanResultWrapper');
var errorDiv = document.getElementById('errorMessage');
var finalResultDisplay = document.getElementById('booleanFinalResult');
var binaryResultDisplay = document.getElementById('booleanBinaryResult');
resultDiv.style.display = 'none';
errorDiv.style.display = 'none';
if (!expression) {
errorDiv.innerHTML = "Please enter a logic expression.";
errorDiv.style.display = 'block';
return;
}
try {
var processed = expression.toUpperCase();
// Replace Operators with JS equivalents
processed = processed.replace(/\bAND\b/g, '&&');
processed = processed.replace(/\./g, '&&');
processed = processed.replace(/\bOR\b/g, '||');
processed = processed.replace(/\+/g, '||');
processed = processed.replace(/\bNOT\b/g, '!');
processed = processed.replace(/~/g, '!');
processed = processed.replace(/\bXOR\b/g, '^');
// Replace variables (using boundaries to avoid partial matches)
processed = processed.replace(/\bA\b/g, valA);
processed = processed.replace(/\bB\b/g, valB);
processed = processed.replace(/\bC\b/g, valC);
// Eval the logical expression
// Note: ^ in JS is bitwise XOR, works for true/false as 1/0
var result = eval(processed);
// Convert truthy/falsy to 1/0
var finalBool = !!result;
finalResultDisplay.innerHTML = finalBool ? "TRUE" : "FALSE";
finalResultDisplay.style.color = finalBool ? "#27ae60" : "#c0392b";
binaryResultDisplay.innerHTML = "Binary Output: " + (finalBool ? "1" : "0");
resultDiv.style.display = 'block';
} catch (e) {
errorDiv.innerHTML = "Invalid syntax. Ensure you use A, B, C and standard operators correctly (e.g., A AND B).";
errorDiv.style.display = 'block';
}
}
Understanding Boolean Algebra
Boolean algebra is a branch of mathematics that deals with variables that have only two distinct values: True (1) and False (0). It forms the foundation of modern digital electronics and computer science, allowing engineers to design complex logic circuits using simple gates.
Core Logical Operators
AND (Conjunction): Output is 1 only if all inputs are 1. Represented as A . B or A & B.
OR (Disjunction): Output is 1 if at least one input is 1. Represented as A + B or A | B.
NOT (Negation): Reverses the input. If A is 1, NOT A is 0. Represented as !A or ~A.
XOR (Exclusive OR): Output is 1 only if the inputs are different. Represented as A ^ B.
The Three Fundamental Laws
Law
AND Form
OR Form
Commutative
A . B = B . A
A + B = B + A
Associative
(A . B) . C = A . (B . C)
(A + B) + C = A + (B + C)
Distributive
A . (B + C) = (A . B) + (A . C)
A + (B . C) = (A + B) . (A + C)
Practical Example
Suppose you are designing a security alarm. The alarm (Result) should ring if the Sensor A (Motion) is triggered AND the System B (Armed) is active. The expression is A AND B. If Motion is detected (A=1) but the system is not armed (B=0), the result is 1 AND 0 = 0 (No alarm).
De Morgan's Theorem
One of the most powerful tools in boolean simplification: