🏆 US-Registered Digital Marketing Agency
Advertisement
Advertisement
DEVELOPER

Truth Table Generator — every row, with tautology detection

Type a boolean expression and get its full truth table, sub-expression columns and a verdict on whether it is always true.

Variables are single letters A–Z. Operators: NOT ! ¬, AND & &&, NAND, XOR ^, OR | ||, NOR, -> => IMPLIES, <-> IFF XNOR. Brackets work to any depth.
Textbooks differ. Logic courses usually start with all true; digital-electronics courses usually count up in binary from all false.
Classification
 
0
Variables
0
Rows in the table
0
Rows that come out true
0
Rows that come out false
Tip: if the expression is a tautology, its negation is a contradiction. Wrapping your expression in NOT( … ) is the fastest way to confirm the parser agrees with you.
Advertisement

The truth table generator above parses a real boolean expression — with operator precedence, brackets to any depth and eight operators — and enumerates every combination of its variables. It prints the result for each row, optionally shows a column for every sub-expression so you can see how the value was built up, and tells you whether the formula is a tautology, a contradiction or a contingency.

Arb Digital publishes it with the rest of its free developer tools because the tedious part of a truth table is never the logic. It is remembering that AND binds tighter than OR, that implication associates to the right, and that 2 to the power of the number of variables grows faster than patience does. The parser here is written from scratch in a few dozen lines of vanilla JavaScript, with no libraries and no network requests, so everything you type stays in your browser.

What This Truth Table Generator Does

It performs three separate jobs. First it tokenises your input, turning a string into variables, operators and brackets, and rejecting anything it cannot recognise with a message that says which character caused the problem. Second it parses those tokens into an expression tree using recursive descent, which is what gives correct precedence and bracket handling rather than the left-to-right evaluation a naive implementation produces. Third it walks every assignment of true and false to the variables, evaluates the tree, and renders the results.

The sub-expression columns are the part worth turning on. With them enabled, an expression like (P AND Q) OR NOT R shows a column for P AND Q, a column for NOT R, and then the final column. That converts the table from an answer into a piece of working you can check line by line, which is what makes it useful for homework rather than just for verification.

One boundary worth stating: this page evaluates arbitrary boolean formulas symbolically. Our logic gate calculator works at the level of individual gates and their electrical behaviour, and the bitwise calculator applies these same operators across all the bits of an integer at once rather than to single truth values.

How to Use It

  1. Type an expression using letters for variables. Case does not matter for operator words, and both symbol and word forms work — A & B and A AND B parse identically.
  2. Add brackets wherever you are unsure. They cost nothing and remove every question about precedence.
  3. Leave sub-expression columns on while you are learning and turn them off when you only want the final answer.
  4. Pick a value style and row order to match whatever notation your course or datasheet uses.
  5. Read the classification in the hero. Tautology means every row is true, contradiction means every row is false, contingency means it depends on the inputs.

The Precedence Rules This Parser Uses

Operators bind in this order, tightest first: NOT, then AND and NAND, then XOR, then OR and NOR, then implication, then the biconditional. NOT is right-associative, so NOT NOT A is legal and equals A. Implication is right-associative, so A -> B -> C means A -> (B -> C), which is the standard convention and is not the same formula as the left-associated reading. Every other binary operator is left-associative.

That means A OR B AND C parses as A OR (B AND C), exactly as multiplication binds tighter than addition in arithmetic. The analogy is not a coincidence: AND is often written as a product and OR as a sum precisely because they follow the same precedence and the same distributive law. If you meant the other grouping, brackets are the only way to say so.

The placement of XOR between AND and OR is a convention rather than a universal law, and different textbooks and programming languages disagree about it. C and its descendants put XOR between AND and OR for bitwise operators, which is where this tool's choice comes from. Because the disagreement is real, the parser shows you the fully bracketed form of what it parsed underneath the classification — if that bracketing is not what you intended, add your own brackets and the ambiguity disappears.

Advertisement

Reading the Default Example Row by Row

Take (P AND Q) OR NOT R. Three variables give eight rows. The formula is true whenever P and Q are both true, and also whenever R is false, regardless of P and Q. Counting: the four rows with R false are all true. Of the four rows with R true, only the one where P and Q are both true is true. So five of the eight rows are true and three are false, and the expression is a contingency — it is neither always true nor always false.

That count is worth doing by hand once, because it is the check that tells you the tool and your understanding agree. If you expected six or seven, the usual cause is reading NOT R as applying to the whole right-hand side. NOT is a unary operator and binds only to the thing immediately after it, which here is the single variable R.

Why the Material Conditional Surprises People

The implication A -> B is false in exactly one case: A true and B false. Every other row, including both rows where A is false, comes out true. That produces the result students find hardest to accept — a false antecedent makes the whole implication true, so "if the moon is made of cheese then I am the King of Spain" is a true statement in classical logic.

The reason is that the material conditional is defined as a truth function: its value depends only on the truth values of its parts, never on any connection between them. It is not trying to capture causation, relevance or "if" as used in ordinary speech. The semantics of these truth-functional connectives are set out precisely in the Stanford Encyclopedia's article on classical logic, which defines the conditional as holding whenever the antecedent fails or the consequent holds.

The practical consequence for anyone writing code or specifications: a vacuously true implication is still true, and a test that only ever exercises rows with a false antecedent proves nothing about the interesting case. If you are checking a rule of the form "if the account is locked then no transfer may occur", the row you need is the one where the account is locked.

NAND, NOR and Functional Completeness

NAND and NOR are not conveniences. Each one on its own is functionally complete, meaning every boolean function whatsoever can be built from copies of just that one operator. NOT A is A NAND A. A AND B is NOT (A NAND B), which is (A NAND B) NAND (A NAND B). Once you have NOT and AND you have everything, because OR follows by De Morgan's law.

That is why physical hardware is dominated by NAND and NOR gates: a fabrication process that can make one universal gate reliably can make any logic at all, and in CMOS a NAND gate needs fewer transistors than an AND gate, because AND is literally a NAND followed by an inverter. MIT's open course 6.004 Computation Structures builds up from this digital abstraction through to complete processors, and it is the clearest free treatment of why gate choice is an engineering decision rather than a notational one.

You can verify functional completeness in this tool directly. Enter (A NAND A) and compare it against NOT A; enter (A NAND B) NAND (A NAND B) and compare against A AND B. Identical columns mean the formulas are logically equivalent.

Proving Two Formulas Equivalent

There is a reliable trick for equivalence that needs only this page. Two formulas are equivalent exactly when their biconditional is a tautology. So instead of eyeballing two tables side by side, enter (formula one) <-> (formula two) and check whether the classification says tautology. If it does, they agree on every row; if it says contingency, at least one row differs and the table will show you which.

The same trick proves De Morgan's laws in one line each: NOT (A AND B) <-> (NOT A OR NOT B) and NOT (A OR B) <-> (NOT A AND NOT B) both come out as tautologies. Distribution, absorption, contraposition and the definition of XOR in terms of AND, OR and NOT all check the same way. This is exhaustive verification rather than proof by derivation, but for a fixed finite number of variables the two are equally conclusive.

The limit is size. Each additional variable doubles the table, so ten variables produce 1,024 rows and twenty produce over a million. This tool caps the number of variables to keep the page responsive, and that cap is a feature: beyond a handful of variables, exhaustive enumeration is the wrong technique and a satisfiability solver is the right one. Our binary arithmetic calculator and number base converter are the tools for the numeric side of the same problems.

Business rules turning into spaghetti?

Arb Digital builds web applications where the conditional logic is specified before it is coded. If your rules have grown past what anyone can reason about, we can help untangle them.

See Web Design & Development Talk to Arb Digital

Common Mistakes to Avoid

  • Assuming left-to-right evaluation — A OR B AND C is A OR (B AND C), not (A OR B) AND C. The two disagree on several rows.
  • Stretching NOT across a whole clause — NOT binds only to the term immediately after it. Use brackets if you mean to negate more.
  • Reading implication as causation — the material conditional is a truth function and comes out true whenever its antecedent is false.
  • Associating implication to the left — A -> B -> C means A -> (B -> C), and the left-associated reading is a different formula.
  • Comparing two tables by eye — join the formulas with a biconditional and check for a tautology instead, which cannot miss a row.

Related Free Tools From Arb Digital

Work at gate level with the logic gate calculator, apply the same operators across whole integers with the bitwise calculator, add and subtract in base two with the binary arithmetic calculator, and move between bases with the number base converter. For signed integer representations, the two's complement converter covers what happens when the top bit means something different. Everything else is in the free online tools hub.

Frequently Asked Questions

What operators does this truth table generator support?

NOT, AND, NAND, XOR, OR, NOR, implication and the biconditional. Each accepts both a word form and a symbol form, so A AND B and A & B parse to the same expression tree.

What is the precedence order?

Tightest to loosest: NOT, then AND and NAND, then XOR, then OR and NOR, then implication, then the biconditional. NOT and implication associate to the right; every other binary operator associates to the left.

How many rows will my table have?

Two raised to the number of distinct variables. Three variables give eight rows, five give thirty-two and ten give 1,024. The tool caps the variable count so the page stays responsive.

What does tautology mean here?

A tautology is a formula that comes out true on every row, whatever values the variables take. A contradiction is false on every row, and a contingency is true on some rows and false on others.

How do I check whether two formulas are equivalent?

Join them with the biconditional operator and build the table. If the result is classified as a tautology, the two formulas agree on every possible assignment and are logically equivalent.

Why is a false antecedent enough to make an implication true?

Because the material conditional is defined purely as a truth function. It is false only when the antecedent is true and the consequent is false, so both rows with a false antecedent evaluate to true.

Is anything I type sent to a server?

No. The tokeniser, the parser and the evaluator all run in your browser as plain JavaScript, and the page makes no network request after it loads.

Advertisement
Advertisement

Take it further