AND, OR, NOT operations. Truth tables and logical equivalence.
Self-serve tutorial - low prerequisites, straightforward concepts.
Every time a program checks a password, a search engine filters results, or a circuit decides whether to light an LED, it’s doing the same thing: combining TRUE/FALSE facts with a small set of rules. Boolean logic is that rulebook.
Boolean logic works with two truth values (TRUE, FALSE) and three core operations: NOT (!), AND (&&), OR (||). Truth tables list outputs for all possible inputs. Logical equivalence means two expressions always produce the same truth value, so you can simplify or rewrite conditions safely.
Boolean logic is a way to compute with truth values.
Instead of numbers like 7 or 3.14, the “data type” here is:
A Boolean expression is anything that evaluates to TRUE or FALSE.
Why care? Because many real questions are naturally yes/no:
In computing, these yes/no questions drive control flow:
if statementswhile loopsIn math, the same ideas show up in proofs:
A proposition is a statement that is definitively TRUE or FALSE.
Examples:
Non-examples (not propositions):
Natural language has words like “and”, “or”, “not”, “if”, “only if”, “unless”. Boolean logic begins with the simplest three:
We’ll use the programming-style symbols specified in this node:
! for NOT&& for AND|| for ORYou should read them as:
!P : “not P”P && Q : “P and Q”P || Q : “P or Q”Each operator takes input truth values and produces an output truth value.
A truth table is how we define these operators precisely: list all possible inputs, and specify the output for each case.
That’s the foundation: Boolean logic is “functions on {TRUE, FALSE}”.
Truth tables matter because they remove ambiguity.
In everyday speech, “or” can be confusing:
In Boolean logic, || is the inclusive OR: it is TRUE when at least one input is TRUE.
We’ll use these conventions:
NOT flips the truth value.
| P | !P |
|---|---|
| T | F |
| F | T |
Interpretation: if P is TRUE, then “not P” is FALSE, and vice versa.
AND is TRUE only when both inputs are TRUE.
| P | Q | P && Q |
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | F |
| F | F | F |
Interpretation: P && Q means “P and Q are both the case.”
OR is TRUE when at least one input is TRUE.
| P | Q | P | Q | |
|---|---|---|---|---|
| T | T | T | ||
| T | F | T | ||
| F | T | T | ||
| F | F | F |
Interpretation: P || Q means “P is the case, or Q is the case, or both.”
Real conditions are usually built from smaller ones:
(!P) && QP && (Q || R)A good habit: compute intermediate columns.
For example, to evaluate (!P) && Q, make a column for !P, then a column for (!P) && Q.
| P | Q | !P | (!P) && Q |
|---|---|---|---|
| T | T | F | F |
| T | F | F | F |
| F | T | T | T |
| F | F | T | F |
This is “showing your work” in logic.
In most programming languages, ! binds tighter than &&, which binds tighter than ||.
So typically:
!P && Q || R is read as ((!P) && Q) || RBut relying on precedence can be risky for learners and teammates.
Use parentheses when learning:
((!P) && Q) || R if that’s what you mean.Here’s a compact way to remember behavior:
| Operator | Symbol | When result is TRUE | Common English | ||
|---|---|---|---|---|---|
| NOT | ! | when input is FALSE | “not” | ||
| AND | && | when both are TRUE | “and” | ||
| OR | ` | ` | when at least one is TRUE | “or (inclusive)” |
Truth tables are the “ground truth” for all later topics: proofs, algebraic simplification, satisfiability, and circuit design.
Once you know what an expression means (via truth tables), the next power is to recognize when two different-looking expressions always give the same result.
That idea is called logical equivalence.
Two Boolean expressions A and B are logically equivalent if they evaluate to the same truth value for every assignment of truth values to their variables.
We write:
Why this matters:
At Difficulty 1, the main tool is: compare truth tables.
Process:
These are the “algebra rules” of Boolean logic. You don’t need to memorize all of them immediately, but you should get comfortable using them.
Negating twice returns the original:
Truth table check:
| P | !P | !!P |
|---|---|---|
| T | F | T |
| F | T | F |
The last column equals P.
These explain how NOT interacts with AND/OR.
Intuition:
Truth table for the first law:
| P | Q | P && Q | !(P && Q) | !P | !Q | (!P) | (!Q) | |
|---|---|---|---|---|---|---|---|---|
| T | T | T | F | F | F | F | ||
| T | F | F | T | F | T | T | ||
| F | T | F | T | T | F | T | ||
| F | F | F | T | T | T | T |
The columns !(P && Q) and (!P) || (!Q) match row-by-row, so they’re equivalent.
Think of TRUE and FALSE as special “absorbing” values.
You can confirm each via a 2-row truth table.
These are why we can write P && Q && R without worrying about parentheses (conceptually), though you should still be careful in code with mixed operators.
People sometimes mean “one or the other but not both.” That operator is XOR.
This node does not include XOR, but it’s worth knowing that plain || is inclusive.
If you ever need XOR using only !, &&, ||, one common form is:
You can verify it with a truth table when you’re ready.
Equivalence laws let you rewrite step-by-step.
Example goal: simplify !(P || Q) || Q.
We can rewrite using De Morgan:
From here, a full simplification would use additional distributive/absorption laws (often taught next). For now, the key skill is: you can legally replace !(P || Q) with (!P) && (!Q) because they are equivalent.
At this stage, truth tables are your safety net: if you’re unsure, compute and compare.
Boolean logic is a shared language between programming, math proofs, and hardware.
if and whileA program constantly evaluates Boolean expressions:
if (isAdmin && !isBanned) { ... }while (hasWork || retryCount < 3) { ... }Truth tables help you reason about edge cases.
Example: Suppose
Then:
P && Q means “logged in AND premium”P || Q means “logged in OR premium (or both)”A common design question is: do you want to allow premium users who are not logged in? If not, P || Q is wrong.
In proofs, you often transform statements using logic.
Even though implication (→) isn’t covered in this node, the mindset is already here:
Digital circuits represent TRUE/FALSE as voltage levels. Gates compute the same operations:
| Logic | Circuit gate | Meaning | ||
|---|---|---|---|---|
! | NOT gate | flips signal | ||
&& | AND gate | both inputs high ⇒ output high | ||
| ` | ` | OR gate | any input high ⇒ output high |
This is why truth tables are also used in hardware design: they specify exactly what a gate or circuit should do.
If two expressions are equivalent, you can swap them without changing behavior.
Example: !(P && Q) is sometimes easier to implement or reason about as (!P) || (!Q) (De Morgan), depending on what signals are available.
When a Boolean condition seems wrong:
This is slow at first, but it’s extremely reliable—and it scales to more advanced logic later.
Let P and Q be Boolean variables. Build a truth table for the expression E = (!P) || (P && Q).
List all possible (P, Q) pairs:
Create intermediate columns for !P and (P && Q).
Compute row by row:
| P | Q | !P | P && Q | E = (!P) | (P && Q) | |
|---|---|---|---|---|---|---|
| T | T | F | T | F | T = T | |
| T | F | F | F | F | F = F | |
| F | T | T | F | T | F = T | |
| F | F | T | F | T | F = T |
Read the result column: E is FALSE only when P = T and Q = F. In all other cases E is TRUE.
Insight: Intermediate columns prevent mistakes. You can see exactly which part of the condition “makes it TRUE” on each row.
Show that !(P || Q) ≡ (!P) && (!Q) by comparing their truth tables.
List all possible (P, Q) pairs:
Compute the left-hand side L = !(P || Q):
First compute (P || Q), then negate.
Compute the right-hand side R = (!P) && (!Q):
First compute !P and !Q, then AND them.
Fill the full table:
| P | Q | P | Q | L = !(P | Q) | !P | !Q | R = (!P) && (!Q) | ||
|---|---|---|---|---|---|---|---|---|---|---|
| T | T | T | F | F | F | F | ||||
| T | F | T | F | F | T | F | ||||
| F | T | T | F | T | F | F | ||||
| F | F | F | T | T | T | T |
Compare columns L and R: they match in every row, so the expressions are equivalent:
!(P || Q) ≡ (!P) && (!Q).
Insight: De Morgan’s Laws are not “tricks”—they are facts defined by the truth tables. Whenever you negate a grouped condition, you flip AND ↔ OR and negate each part.
A website rule says: “A user can access the page if they are an admin OR (they are logged in AND not banned).”
Let:
Write the Boolean expression and test two scenarios.
Translate the sentence carefully:
E = A || (L && !B)
Scenario 1: A = F, L = T, B = F (not admin, logged in, not banned)
Compute:
So access is allowed.
Scenario 2: A = F, L = T, B = T (not admin, logged in, banned)
Compute:
So access is denied.
Insight: Parentheses encode the policy. Without them, a small change in grouping can create a security bug.
Boolean logic computes with two values: TRUE and FALSE.
NOT ! flips a truth value; AND && is TRUE only if both inputs are TRUE; OR || is TRUE if at least one input is TRUE.
Truth tables precisely define operators and are the most reliable way to check your reasoning.
Use intermediate columns in truth tables to avoid errors in complex expressions.
Two expressions are logically equivalent (A ≡ B) if they match on every row of their truth tables.
De Morgan’s Laws: !(P && Q) ≡ (!P) || (!Q) and !(P || Q) ≡ (!P) && (!Q).
Equivalences let you rewrite conditions safely in code, circuits, and proofs.
Confusing inclusive OR (||) with exclusive or (“either but not both”). In Boolean logic, P || Q is TRUE when both are TRUE.
Forgetting parentheses and relying on operator precedence, leading to a different meaning than intended.
Negating a grouped condition incorrectly (e.g., writing !(P && Q) as (!P) && (!Q) instead of (!P) || (!Q)).
Skipping intermediate columns in truth tables and making arithmetic-like “mental leaps” that hide errors.
Create the truth table for E = P && (!Q).
Hint: Make a column for !Q, then AND it with P.
Truth table:
| P | Q | !Q | P && (!Q) |
|---|---|---|---|
| T | T | F | F |
| T | F | T | T |
| F | T | F | F |
| F | F | T | F |
E is TRUE only when P is TRUE and Q is FALSE.
Use a truth table to decide whether these are equivalent: !(P && Q) and (!P) || (!Q).
Hint: Compute both expressions for all four (P, Q) rows and compare the final columns.
They are equivalent. Truth table:
| P | Q | P && Q | !(P && Q) | !P | !Q | (!P) | (!Q) | |
|---|---|---|---|---|---|---|---|---|
| T | T | T | F | F | F | F | ||
| T | F | F | T | F | T | T | ||
| F | T | F | T | T | F | T | ||
| F | F | F | T | T | T | T |
The last two columns match.
A rule says: “You can submit the form only if you are logged in and you have either accepted the terms or you are an admin.”
Let L = logged in, T = accepted terms, A = admin. Write a Boolean expression using only !, &&, ||. Then evaluate it for (L, T, A) = (T, F, F) and (T, F, T).
Hint: Translate “only if” here as a requirement: the condition must be TRUE to submit. Group “accepted terms or admin” with parentheses.
Expression:
E = L && (T || A)
Evaluate (L, T, A) = (T, F, F):
So cannot submit.
Evaluate (L, T, A) = (T, F, T):
So can submit.