Boolean Algebra Calculator

Boolean Algebra Calculator

Enter binary values (0 or 1) for variables A, B, and C, then input a Boolean expression using these variables and operators (AND, OR, NOT, XOR). The calculator will evaluate the expression.

0 1
0 1
0 1

Result:

Understanding Boolean Algebra

Boolean algebra is a fundamental branch of mathematics and logic that deals with binary variables, which can only take on one of two values: true (1) or false (0). It is the mathematical foundation of digital circuit design, computer programming, and mathematical logic. Unlike elementary algebra where variables represent numbers, in Boolean algebra, variables represent propositions or conditions.

Basic Boolean Operations:

  • AND (Conjunction): The output is 1 (true) only if all inputs are 1. If any input is 0, the output is 0. (e.g., A AND B)
  • OR (Disjunction): The output is 1 (true) if at least one input is 1. The output is 0 only if all inputs are 0. (e.g., A OR B)
  • NOT (Negation): This is a unary operator that inverts the input. If the input is 1, the output is 0, and vice versa. (e.g., NOT A)
  • XOR (Exclusive OR): The output is 1 (true) if the inputs are different (one is 0 and the other is 1). The output is 0 if the inputs are the same (both 0 or both 1). (e.g., A XOR B)

Operator Precedence:

Just like in standard algebra, Boolean operations have an order of precedence that determines the sequence of evaluation. This calculator uses JavaScript's operator precedence for evaluation. The standard Boolean precedence is generally: NOT > AND > OR > XOR.

While NOT, AND, and OR generally align with standard Boolean algebra precedence in this calculator, the XOR operator (represented by ^ in JavaScript) has a lower precedence than AND and OR in JavaScript. To ensure correct evaluation according to standard Boolean algebra rules, it is highly recommended to use parentheses to explicitly group operations, especially when XOR is combined with AND or OR (e.g., (A AND B) XOR C for A AND B first, then XOR with C).

How to Use the Calculator:

  1. Set Variable Values: Choose '0' or '1' for variables A, B, and C using the dropdown menus.
  2. Enter Boolean Expression: Type your Boolean expression into the "Boolean Expression" field. You can use variables A, B, C, and the operators AND, OR, NOT, XOR. Parentheses () can be used to group operations and control precedence.
  3. Click Calculate: The result (0 or 1) will be displayed below.

Examples:

  • Example 1: Simple AND operation
    • Variables: A=1, B=0, C=any
    • Expression: A AND B
    • Calculation: 1 AND 0 evaluates to 0
    • Result: 0
  • Example 2: Using NOT and OR
    • Variables: A=0, B=1, C=0
    • Expression: NOT A OR B
    • Calculation: NOT 0 OR 1 evaluates to 1 OR 1, which is 1
    • Result: 1
  • Example 3: Complex expression with parentheses
    • Variables: A=1, B=0, C=1
    • Expression: A AND (B OR NOT C)
    • Calculation: 1 AND (0 OR NOT 1) evaluates to 1 AND (0 OR 0), which is 1 AND 0, resulting in 0
    • Result: 0
  • Example 4: Using XOR (mind precedence)
    • Variables: A=1, B=1, C=0
    • Expression: (A AND C) XOR B
    • Calculation: (1 AND 0) XOR 1 evaluates to 0 XOR 1, resulting in 1
    • Result: 1
.calculator-container { font-family: Arial, sans-serif; max-width: 600px; margin: 20px auto; padding: 20px; border: 1px solid #ccc; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); background-color: #f9f9f9; } .calculator-container h2, .calculator-container h3 { color: #333; text-align: center; margin-bottom: 15px; } .input-group { margin-bottom: 15px; } .input-group label { display: block; margin-bottom: 5px; font-weight: bold; color: #555; } .input-group input[type="text"], .input-group select { width: calc(100% – 22px); padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; } button { display: block; width: 100%; padding: 12px 20px; background-color: #007bff; color: white; border: none; border-radius: 4px; font-size: 18px; cursor: pointer; transition: background-color 0.3s ease; } button:hover { background-color: #0056b3; } .result-container { margin-top: 20px; padding: 15px; border: 1px solid #e0e0e0; border-radius: 4px; background-color: #e9ecef; text-align: center; } .result-container h3 { margin-top: 0; color: #333; } #result { font-size: 24px; font-weight: bold; color: #28a745; } .calculator-article { margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee; } .calculator-article h3 { text-align: left; color: #333; margin-bottom: 10px; } .calculator-article h4 { color: #444; margin-top: 15px; margin-bottom: 8px; } .calculator-article p, .calculator-article ul, .calculator-article ol { line-height: 1.6; color: #666; margin-bottom: 10px; } .calculator-article ul li, .calculator-article ol li { margin-bottom: 5px; } function calculateBoolean() { var varA = document.getElementById("varA").value; var varB = document.getElementById("varB").value; var varC = document.getElementById("varC").value; var expression = document.getElementById("booleanExpression").value; var resultDiv = document.getElementById("result"); // Basic validation for variable inputs (should always be 0 or 1 from dropdowns) if (!/^[01]$/.test(varA) || !/^[01]$/.test(varB) || !/^[01]$/.test(varC)) { resultDiv.innerHTML = "Error: Variable values must be 0 or 1."; return; } var vars = { 'A': parseInt(varA), 'B': parseInt(varB), 'C': parseInt(varC) }; try { var evaluatedResult = evaluateBooleanExpression(expression, vars); resultDiv.innerHTML = evaluatedResult; } catch (e) { resultDiv.innerHTML = "Error: " + e.message + ""; } } function evaluateBooleanExpression(expression, vars) { var jsExpression = expression.toUpperCase(); // Step 1: Replace textual operators with JavaScript equivalents // Replace NOT first, as it's unary and has highest precedence // Regex looks for 'NOT' followed by a variable (A-Z), a binary digit (0-1), or an opening parenthesis. jsExpression = jsExpression.replace(/NOT\s*([A-Z0-1(])/g, '!$1'); // Replace AND, OR, XOR jsExpression = jsExpression.replace(/AND/g, '&&'); jsExpression = jsExpression.replace(/OR/g, '||'); jsExpression = jsExpression.replace(/XOR/g, '^'); // Bitwise XOR, behaves as logical XOR for 0/1 // Step 2: Replace variables with their numerical values for (var key in vars) { if (vars.hasOwnProperty(key)) { // Use regex to replace whole words only, ensuring it's not part of another word jsExpression = jsExpression.replace(new RegExp('\\b' + key + '\\b', 'g'), vars[key]); } } // Step 3: Basic sanitization and validation before eval() // This regex ensures that only allowed characters remain after replacements. // Allowed: 0, 1, (, ), !, &, |, ^, and spaces. var validCharsRegex = /^[01\(\)\!\&\|\^\s]*$/; if (!validCharsRegex.test(jsExpression)) { throw new Error("Invalid characters or syntax in expression. Only 0, 1, (, ), !, &&, ||, ^ are allowed after variable substitution."); } // Check for balanced parentheses var openParen = (jsExpression.match(/\(/g) || []).length; var closeParen = (jsExpression.match(/\)/g) || []).length; if (openParen !== closeParen) { throw new Error("Unbalanced parentheses in expression."); } // Check for empty expression if (jsExpression.trim() === "") { throw new Error("Expression cannot be empty."); } // Try to evaluate the constructed JavaScript expression try { // eval() will treat 0 as false and 1 as true for logical operators (&&, ||). // Bitwise XOR (^) works directly on numbers (0 and 1). var result = eval(jsExpression); // Convert true/false results from logical operations back to 1/0 for consistency in Boolean algebra return result ? 1 : 0; } catch (e) { throw new Error("Syntax error in Boolean expression. Please check your input. Details: " + e.message); } }

Leave a Reply

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