Learning objectives
By the end of this week you can:
- 1 Use the biconditional and exclusive or, state when each is true, and rewrite each with ¬, ∧, ∨.
- 2 Decide whether two formulas are logically equivalent, with a truth table and with a chain of laws.
- 3 Simplify a compound proposition step by step, naming the law used at every step.
- 4 Classify formulas as tautology, contradiction or contingency, and decide whether a set of statements is satisfiable (consistent).
- 5 Solve small logic puzzles and check system specifications with propositional logic.
- 6 Build half and full adders from logic gates and explain how a CPU adds binary numbers.
★ the wow moment
Your CPU adds numbers with the logic you learned last week.
A one-bit adder is just two formulas: sum = p ⊕ q and carry = p ∧ q. Chain 64 of them and you have the adder in your laptop. On this page you click the inputs of a real full adder and watch a 4-bit adder ripple its carry; in the lab you build it in Python.
Where we are
Last week we built compound propositions and their truth tables. This week asks the next natural question: when do two different-looking formulas say the same thing? Answering it gives us three tools you will use for the rest of your career:
- Equivalence. Replacing a formula with a simpler one that means exactly the same, which is what refactoring a condition in code really is.
- Satisfiability. Deciding whether a set of requirements can all be true at once.
- Circuits. Seeing that a formula is a piece of hardware.
We start by looking more closely at the two connectives we met briefly last week, ↔ and ⊕.
The biconditional, in depth
| p | q | p → q | q → p | p ↔ q |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 |
| 0 | 0 | 1 | 1 | 1 |
The table shows where the name comes from: is the conditional in both directions, . That is why mathematicians use it to state definitions and characterizations: is both sufficient and necessary for .
Four properties are worth knowing by heart (T is “always true”, F is “always false”):
| Property | Why |
|---|---|
| a statement always agrees with itself | |
| a statement never agrees with its negation | |
| “ iff true” is just | |
| “ iff false” is “not ” |
In code, the biconditional of two booleans is plain equality: p == q.
Exclusive or, in depth
XOR can be written with the basic connectives in two standard ways. Read them aloud and they make sense:
Its algebra is what makes it so useful in computing:
| Property | Meaning |
|---|---|
| XOR with 0 changes nothing | |
| XOR with 1 flips the bit | |
| anything XOR itself is 0 | |
| , | order and grouping do not matter |
| XOR twice with the same key gives the original back |
Tautologies, contradictions, contingencies
| Formula | Kind |
|---|---|
| tautology (law of excluded middle) | |
| tautology | |
| contradiction | |
| contradiction | |
| contingency (true in one row of four) |
There are two ways to decide which kind a formula is: build its truth table, or simplify it with the laws below until you reach T, F or something that is clearly neither. The truth table always works but grows as ; simplification is shorter, and it is what you do on paper and in your head.
Logical equivalence
Method 1: the truth table
Put both formulas side by side and compare the columns. Is ?
| p | q | p ∧ q | ¬(p ∧ q) | ¬p | ¬q | ¬p ∨ ¬q |
|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 0 | 1 |
| 0 | 0 | 0 | 1 | 1 | 1 | 1 |
The two result columns are identical, so they are equivalent. This is De Morgan’s law. To show two formulas are not equivalent, one row is enough: a single counterexample.
Try it yourself. Change the formulas, or click a preset, and the checker either confirms the equivalence or shows the row where they disagree:
Method 2: the laws of logic
Truth tables do not scale and do not explain why. The second method is algebra: rewrite one formula into the other using equivalences that are already known to hold. Here are the ones you need; T and F stand for any tautology and any contradiction.
| Law | Form with ∧ | Form with ∨ |
|---|---|---|
| Identity | ||
| Domination | ||
| Idempotent | ||
| Double negation | ||
| Commutative | ||
| Associative | ||
| Distributive | ||
| De Morgan | ||
| Absorption | ||
| Negation |
Notice the duality: every law in the ∧ column turns into the law in the ∨ column if you swap ∧ with ∨ and T with F. Learn one column and you get the other for free.
The conditional and the biconditional have their own equivalences. The first one is the most used line in this whole course:
| Equivalence | Name or meaning |
|---|---|
| conditional as a disjunction | |
| contrapositive | |
| how a promise is broken | |
| “if not this, then that” | |
| one hypothesis, two conclusions | |
| proof by cases (week 7) | |
| exportation | |
| biconditional as two conditionals | |
| negating a biconditional |
Proving equivalences step by step
A proof by equivalences is a chain where every step names a law. Write one step per line, like code, so that a reader can check each line on its own.
Simplification
Simplifying means the same chain of laws, but you do not know the target in advance: you stop when nothing shorter is possible. These come from the lecture slides, each with the law used at every step.
Satisfiability and SAT
Is satisfiable? Read it as a cycle: , , . It is true when are all equal, so yes, for example with all three true. Now add “at least one is true” and “at least one is false”:
The first three force all variables to be equal; the last two forbid all-true and all-false. Nothing is left: the formula is unsatisfiable. Paste it into the playground below as (p | ~q) & (q | ~r) & (r | ~p) & (p | q | r) & (~p | ~q | ~r) and watch the verdict turn into “contradiction”.
~ not · & and · | or · ^ xor · -> implies · <-> iff · variables p q r s …
python ›
Logic at work: specifications and puzzles
Are these requirements consistent?
Specifications are written in English, so engineers translate them into logic to check that they do not contradict each other. Let : the user is authenticated, : access is granted, : the account is locked.
- Access is granted only if the user is authenticated: .
- If the account is locked, access is not granted: .
- Authenticated users are granted access: .
These three are consistent: for example , satisfies all of them. Now a customer asks for a fourth requirement:
- Some authenticated user has a locked account: .
From 4 and 3 we get ; from 4 and 2 we get . The four requirements are inconsistent: no system can meet all of them, and finding that out on paper is far cheaper than finding it in production.
Knights and Knaves
On an island, knights always tell the truth and knaves always lie (Raymond Smullyan’s puzzles). You meet A and B.
- A says: “B is a knight.”
- B says: “The two of us are of opposite types.”
Let : A is a knight, : B is a knight. A’s statement is true exactly when A is a knight, so . B’s statement gives . We need both:
The first part forces ; then is false, so the second part forces to be false. Both are knaves. Type (p <-> q) & (q <-> (p ^ q)) into the playground: exactly one row, , is true. The pattern “a speaker is a knight iff what they say is true” turns every such puzzle into a satisfiability problem. In Lab 2 you write a program that solves any of them.
Boolean search
Library catalogs and advanced search engines take Boolean queries: discrete AND (mathematics OR math) NOT music. Everything you know about precedence and De Morgan applies; in particular, NOT (a OR b) returns the same results as NOT a AND NOT b.
Logic circuits
A logic gate is a tiny electronic circuit that computes one connective on voltages: high voltage is 1, low is 0.
| Gate | Output | Formula |
|---|---|---|
| NOT (inverter) | the opposite of the input | |
| AND | 1 when both inputs are 1 | |
| OR | 1 when at least one input is 1 | |
| XOR | 1 when the inputs differ | |
| NAND | NOT of AND |
Every formula is a circuit: each connective becomes a gate, and each subformula becomes a wire. Simplifying the formula removes gates, which is why chip designers care so much about the laws above.
NAND can do everything
The set can express every truth table: write one ∧-term for each true row and join them with ∨ (the disjunctive normal form, a Lab 1 challenge). Surprisingly, a single gate is enough. With for NAND:
Check the last one with De Morgan. Because NAND is functionally complete, real chips are built from millions of copies of one or two gate designs.
Adding binary numbers
Adding two bits produces a sum bit and a carry bit: , so sum 0, carry 1.
| A | B | carry = A ∧ B | sum = A ⊕ B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 0 |
That table is the half adder: one XOR and one AND. To add longer numbers, each column must also add the carry coming from the column on its right. The full adder does this with three inputs :
Click the inputs and follow the signal through the gates:
Chain four of them: a 4-bit ripple-carry adder
Read right to left: each column is one full adder. Its carry-out is the next column's carry-in, so the carry “ripples” from bit 0 to bit 3. A carry out of bit 3 means the sum needs a fifth bit: that is overflow in a 4-bit register.
Four full adders in a row, each one’s carry-out wired to the next one’s carry-in, make a ripple-carry adder. A 64-bit processor does the same with 64 of them (plus tricks to make the carry faster). Everything your computer computes, from video to AI, is built on top of this one page of logic.
Summary
- : same truth value. : different truth value.
- XOR facts: , , , and XOR-ing twice with a key gives the original back.
- means the same truth value in every row, that is, is a tautology. One differing row (a counterexample) proves non-equivalence.
- Prove and simplify with a chain of named laws. Strategy: remove → and ↔, push ¬ inwards, then collapse.
- Satisfiable means at least one true row; a set of requirements is consistent when its conjunction is satisfiable. SAT is hard in theory and solved every day in practice.
- Every formula is a circuit. Half adder: , . Full adders in a chain add binary numbers.
Practice
Answers
- (conditional twice, then associative and commutative, then conditional twice back).
- (De Morgan with double negation, distributive, negation, identity).
- Yes, it is a tautology. If leads to and leads to , then leads to : this is hypothetical syllogism, one of the rules of inference in week 6.
- Let : A is a knight, : B is a knight. Then . If were false, the statement would be true, so a knave would have said something true: impossible. So is true, the statement must be true, and holds. A is a knight, B is a knave.
- Let . Then , and , which is that result NAND-ed with itself. Five NAND gates in total.
Before the lab
Lab 2 turns this page into Python. You will check equivalences by brute force, build half and full adders from gate functions and add real binary numbers with them, and write a solver for Knights and Knaves puzzles. It builds directly on the truth-table engine you wrote in Lab 1.
In industry
-
Chip design
Hardware tools minimize Boolean formulas with exactly these laws, so that chips use fewer transistors, less power and less time.
-
SAT solvers
Satisfiability solvers verify processors, plan logistics and solve Sudoku by searching for a truth assignment, often with millions of variables.
-
Clean code
Refactoring an if-condition is applying De Morgan, distributivity and absorption. A wrong step is a bug that only shows up in one row of the truth table.
-
Storage and security
XOR parity lets RAID disks rebuild a dead drive, and XOR with a secret key is the core step of most ciphers.
Lab 2: Gates, Adders and Knights & Knaves
Build logic gates, check equivalences by brute force, add binary numbers with a ripple-carry adder made of gates, prove that NAND is enough, and write a solver for Knights and Knaves puzzles.
due Fri, Oct 9 · 23:59 · 100 points · open now
Go deeper
- Stanford CS103 truth table tool · check equivalences you derive by hand
- The Nand Game · build a whole computer, starting from NAND gates; the first levels are this week
- Adder (electronics), Wikipedia · half, full and ripple-carry adders
- Boolean satisfiability problem, Wikipedia · why SAT is the most famous hard problem
- Discrete Mathematics: An Open Introduction (Oscar Levin) · Section 3.1, propositional logic and equivalences
- Peter Norvig: Solving every Sudoku puzzle · constraint propagation, a close cousin of SAT solving
Tags:equivalencelogical equivalenceDe MorganXORbiconditionaltautologycontradictionsatisfiabilitySATsimplificationlogic gatesadderNANDKnights and Knaves