Boolean Logic Calculator

Boolean Logic Calculator

Use this calculator to explore the fundamental operations of Boolean logic. Simply select the truth values for Input A and Input B, and the calculator will display the results for various logical operations.

function calculateBooleanLogic() { var inputA = document.getElementById('inputA').checked; var inputB = document.getElementById('inputB').checked; var resultDiv = document.getElementById('booleanResult'); var outputHTML = '

Logic Results:

'; // Helper function to convert boolean to "True" or "False" function boolToString(value) { return value ? 'True' : 'False'; } // AND operation var andResult = inputA && inputB; outputHTML += 'A AND B: ' + boolToString(andResult) + "; // OR operation var orResult = inputA || inputB; outputHTML += 'A OR B: ' + boolToString(orResult) + "; // NOT A operation var notAResult = !inputA; outputHTML += 'NOT A: ' + boolToString(notAResult) + "; // NOT B operation var notBResult = !inputB; outputHTML += 'NOT B: ' + boolToString(notBResult) + "; // XOR operation (A XOR B is (A AND NOT B) OR (NOT A AND B)) var xorResult = (inputA && !inputB) || (!inputA && inputB); outputHTML += 'A XOR B: ' + boolToString(xorResult) + "; // NAND operation (NOT (A AND B)) var nandResult = !(inputA && inputB); outputHTML += 'A NAND B: ' + boolToString(nandResult) + "; // NOR operation (NOT (A OR B)) var norResult = !(inputA || inputB); outputHTML += 'A NOR B: ' + boolToString(norResult) + "; // XNOR operation (NOT (A XOR B)) var xnorResult = !xorResult; // Or (A AND B) OR (NOT A AND NOT B) outputHTML += 'A XNOR B: ' + boolToString(xnorResult) + "; resultDiv.innerHTML = outputHTML; } // Calculate on page load with default values window.onload = calculateBooleanLogic; .boolean-logic-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: 600px; margin: 20px auto; border: 1px solid #ddd; } .boolean-logic-calculator h2 { color: #333; text-align: center; margin-bottom: 20px; } .boolean-logic-calculator p { color: #555; line-height: 1.6; margin-bottom: 15px; } .boolean-logic-calculator .calculator-form { background-color: #ffffff; padding: 20px; border-radius: 8px; border: 1px solid #eee; margin-bottom: 20px; } .boolean-logic-calculator .form-group { display: flex; align-items: center; margin-bottom: 15px; } .boolean-logic-calculator .form-group label { flex: 1; color: #333; font-weight: bold; margin-right: 10px; } .boolean-logic-calculator .form-group input[type="checkbox"] { flex: 2; width: 20px; height: 20px; cursor: pointer; margin-left: auto; /* Pushes checkbox to the right */ } .boolean-logic-calculator button { display: block; width: 100%; padding: 12px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px; font-size: 16px; cursor: pointer; transition: background-color 0.3s ease; margin-top: 10px; } .boolean-logic-calculator button:hover { background-color: #0056b3; } .boolean-logic-calculator .calculator-result { background-color: #e9f7ef; padding: 20px; border-radius: 8px; border: 1px solid #d4edda; margin-top: 20px; } .boolean-logic-calculator .calculator-result h3 { color: #28a745; margin-top: 0; margin-bottom: 15px; text-align: center; } .boolean-logic-calculator .calculator-result p { margin-bottom: 8px; color: #333; font-size: 1.1em; } .boolean-logic-calculator .calculator-result p strong { color: #0056b3; }

Understanding Boolean Logic

Boolean logic, named after mathematician George Boole, is a branch of algebra that deals with true and false values, typically represented as 1 and 0, or True and False. It forms the fundamental basis of all digital circuits, computer programming, and database queries. Unlike traditional algebra that uses numbers and arithmetic operations, Boolean algebra uses logical operators to manipulate truth values.

Key Boolean Operations:

  • AND (Conjunction): The output is True only if ALL inputs are True. If even one input is False, the output is False.
  • OR (Disjunction): The output is True if AT LEAST ONE input is True. The output is False only if ALL inputs are False.
  • NOT (Negation): This is a unary operator, meaning it operates on a single input. It reverses the truth value of its input. If the input is True, the output is False, and vice-versa.
  • XOR (Exclusive OR): The output is True if the inputs are DIFFERENT. If both inputs are the same (both True or both False), the output is False.
  • NAND (NOT AND): The output is the opposite of AND. It is False only if ALL inputs are True; otherwise, it's True.
  • NOR (NOT OR): The output is the opposite of OR. It is True only if ALL inputs are False; otherwise, it's False.
  • XNOR (Exclusive NOR): The output is the opposite of XOR. It is True if the inputs are the SAME; otherwise, it's False.

How Boolean Logic Works in Practice

In computer science, Boolean logic is used extensively for conditional statements (e.g., if (condition1 AND condition2)), database queries (e.g., SELECT * FROM users WHERE age > 18 OR city = 'New York'), and designing digital circuits (logic gates). Each gate in a computer chip performs a basic Boolean operation.

Using the Calculator

This Boolean Logic Calculator allows you to set the truth values for two inputs, A and B, using checkboxes. A checked box represents 'True' (or 1), and an unchecked box represents 'False' (or 0). After selecting your inputs, click the "Calculate Logic" button to see the results for all the primary Boolean operations. This provides an interactive way to understand how different combinations of inputs yield different logical outcomes.

Example Scenarios:

Let's consider a simple example:

  • Scenario 1: Input A = True, Input B = True
    • A AND B: True
    • A OR B: True
    • A XOR B: False
    • NOT A: False
    • NOT B: False
    • A NAND B: False
    • A NOR B: False
    • A XNOR B: True
  • Scenario 2: Input A = True, Input B = False
    • A AND B: False
    • A OR B: True
    • A XOR B: True
    • NOT A: False
    • NOT B: True
    • A NAND B: True
    • A NOR B: False
    • A XNOR B: False

Experiment with different combinations to solidify your understanding of these fundamental logical principles.

Leave a Comment