Mathematics for Computer Science 201-N11 • CEGEP, Montreal

Corrected exercises: Boolean and mathematical logic (201-N11)

This is the corrected exercise set on Boolean logic for Mathematics for Computer Science, 201-N11, the mathematics course taken at CEGEP by students in computer science programs (Computer Science Technology, and Computer Science and Mathematics). Part A covers the basics: the five connectives and their truth tables (exercise 1), the laws of Boolean algebra and simplification (exercise 2), the implication with its contrapositive, its converse and its negation (exercise 3), quantifiers and how negation swaps ∀\forall and ∃\exists (exercise 4), then going from a truth table to the canonical sum-of-products form (exercise 5). Part B moves up to exam level: the four-variable Karnaugh map with groups that wrap around the edges (exercise 6), the half adder and the full adder, and the universality of the NAND gate (exercise 7), an access control system to write and then simplify (exercise 9), and testing the validity of three arguments (exercise 10).

The thread of the set: the truth table is the judge, Boolean algebra is only a shortcut. Whenever a simplification is doubtful, you settle it by writing out the rows; whenever an equivalence is claimed, you prove it by comparing two columns. No intuition drawn from everyday English is worth one row of a truth table.

The pitfalls named explicitly in the solutions: confusing an implication with its converse (exercises 3 and 8), forgetting that De Morgan changes the connective at the same time as it lifts the bar (exercises 2 and 8), believing that the negation of “all” is “none” (exercise 4), and writing an OR where the specification requires a common factor (exercise 9).

10 corrected exercises • 100 points • 150 minutes

This chapter is part of 201-N11, every chapter with its set and sheet Before you start Revision sheet: the traps and the method for this chapter

Self-checking set Type your answers and check them question by question: the page tells you right or wrong before you open the solution. With an account, every answer right on the first try earns points, and your right answers stay filled in when you come back.

Course recap

  • • A truth table with nn propositions has 2n2^{n} rows. There are 22n2^{2^{n}} Boolean functions of nn variables.
  • • p⇒qp \Rightarrow q is false only on the row p=1p=1, q=0q=0. Everywhere else it is true, including vacuously when p=0p=0.
  • • p⇒q=¬p∨qp \Rightarrow q = \lnot p \lor q, and ¬(p⇒q)=p∧¬q\lnot(p \Rightarrow q) = p \land \lnot q.
  • • Contrapositive of p⇒qp \Rightarrow q: ¬q⇒¬p\lnot q \Rightarrow \lnot p, always equivalent. Converse: q⇒pq \Rightarrow p, with no truth connection to the original.
  • • De Morgan: AB‾=A‾+B‾\overline{AB} = \overline{A}+\overline{B} and A+B‾=A‾ B‾\overline{A+B} = \overline{A}\,\overline{B}.
  • • Both distributive laws: A(B+C)=AB+ACA(B+C) = AB+AC and A+BC=(A+B)(A+C)A+BC = (A+B)(A+C).
  • • Absorption: A+AB=AA+AB = A and A(A+B)=AA(A+B) = A. Idempotence: AA=AAA=A and A+A=AA+A=A.
  • • Negating a quantifier: ¬(∀x,P(x))=∃x,¬P(x)\lnot(\forall x, P(x)) = \exists x, \lnot P(x) and ¬(∃x,P(x))=∀x,¬P(x)\lnot(\exists x, P(x)) = \forall x, \lnot P(x).
  • • Canonical sum-of-products form: one term for each row where the function equals 1, with a variable complemented (barred) if it equals 0 on that row.
  • • Karnaugh map: groups are rectangles of 2k2^{k} cells, opposite edges are adjacent, and a group of 2k2^{k} cells eliminates kk variables.

Part A: the basics (/50)

Exercise 1: Logical connectives and truth tables

A proposition is a statement that is either true (1) or false (0), never both. Connectives are defined solely by their truth tables: the table, not your intuition about everyday language, tells you what an expression is worth.

ppqqp∧qp \land qp∨qp \lor qp⇒qp \Rightarrow q
11111
10010
01011
00001
  • a) Build the complete truth table of p∧¬qp \land \lnot q.
  • b) On which row, and on that row alone, is the implication p⇒qp \Rightarrow q false? Translate that row into plain English with the example: “if the file exists, then the read succeeds”.
  • c) Build the table of the exclusive or p⊕qp \oplus q, then that of ¬(p⇔q)\lnot(p \Leftrightarrow q). What do you conclude?
  • d) How many rows does the truth table of five propositions have? How many distinct Boolean functions of two variables are there?

Type your answers, the page tells you right or wrong 0/9

a)
b)
c)
d)
Show the solution

Answers

  • a) ¬q\lnot q is 0,1,0,10,1,0,1 and p∧¬qp\land\lnot q is 0,1,0,00,1,0,0: true on the single row p=1p=1, q=0q=0
  • b) The row p=1p=1, q=0q=0: the promise is broken only if the file exists and the read fails
  • c) Both columns are 0,1,1,00,1,1,0: p⊕qp \oplus q and ¬(p⇔q)\lnot(p \Leftrightarrow q) are the same function
  • d) 25=322^{5}=32 rows and 24=162^{4}=16 Boolean functions of two variables
a)

¬q\lnot q inverts the column of qq, then apply AND, which equals 1 only when both columns equal 1. Rows in the order (p,q)=(1,1),(1,0),(0,1),(0,0)(p,q)=(1,1),(1,0),(0,1),(0,0): ¬q\lnot q is 0,1,0,10,1,0,1 and p∧¬qp\land\lnot q is 0,1,0,00,1,0,0. The expression is therefore true only on the row p=1p=1, q=0q=0. That is exactly the row where p⇒qp\Rightarrow q is false, and it is no coincidence: p∧¬qp\land\lnot q is the negation of p⇒qp\Rightarrow q.

b)

A single row: p=1p=1 and q=0q=0. An implication is false only when the premise is true and the conclusion is false. In the example, the promise “if the file exists, then the read succeeds” is broken only when the file really does exist and the read fails anyway. If the file does not exist, the promise is never tested, and we declare it kept: the last two rows equal 1. This is the point students resist the longest, because the “if” of everyday language carries an idea of cause that the logical ⇒\Rightarrow does not have.

c)

p⊕qp\oplus q equals 1 when the two values differ: 0,1,1,00,1,1,0 on the four rows. p⇔qp\Leftrightarrow q equals 1 when they are equal, that is 1,0,0,11,0,0,1, so its negation is 0,1,1,00,1,1,0. The two columns are identical: p⊕qp\oplus q and ¬(p⇔q)\lnot(p\Leftrightarrow q) are the same Boolean function. Two expressions are equivalent exactly when their tables agree row by row, and this is the only proof of equivalence that requires no trick.

d)

With nn propositions, each proposition takes 2 values independently of the others: the table has 2n2^{n} rows, so 25=322^{5}=32 rows for five propositions. To count the functions of two variables, notice that a function is entirely determined by the column of outputs it produces, that is 4 cells to fill with 0 or 1: there are 24=162^{4}=16 Boolean functions of two variables. They all have a name: AND, OR, XOR, NAND, NOR, the implication, the two constants, and so on. Classic pitfall: answering 4 by confusing the number of rows with the number of functions.

Tick the exercises you have done or want to review: a free account, no password, keeps your ticks and your right answers from one visit to the next, tells you which chapter to tackle next and lets you ask for the exercise you are missing. Create your space, an email is enough.

Exercise 2: Boolean algebra: the laws and simplification

Boolean algebra gives the same answers as the truth table, but without writing out the rows. We write A‾\overline{A} for the negation (complement), the product ABAB for AND and the sum A+BA+B for OR. Every simplification must preserve the table: if the table changes, the simplification is wrong.

  • a) Simplify A+ABA + AB and state the law used.
  • b) Simplify (A+B)(A+B‾)(A+B)(A+\overline{B}).
  • c) Simplify AB‾+A‾\overline{AB} + \overline{A}.
  • d) Simplify A‾+B‾‾⋅(A+B)\overline{\overline{A}+\overline{B}} \cdot (A+B).
  • e) What are A⊕AA \oplus A, A⊕A‾A \oplus \overline{A} and A⊕0A \oplus 0?
  • f) Check the result of question b) by drawing up its truth table.

Type your answers, the page tells you right or wrong 0/10

a)
b)
c)
d)
e)
f)
Show the solution

Answers

  • a) A+AB=AA+AB = A, by absorption
  • b) (A+B)(A+B‾)=A(A+B)(A+\overline{B}) = A
  • c) AB‾+A‾=A‾+B‾=AB‾\overline{AB}+\overline{A} = \overline{A}+\overline{B} = \overline{AB}
  • d) ABAB
  • e) A⊕A=0A \oplus A = 0, A⊕A‾=1A \oplus \overline{A} = 1, A⊕0=AA \oplus 0 = A
  • f) The product is 1,1,0,01,1,0,0: it is the column of AA, the simplification is confirmed
a)

A+AB=A(1+B)=A⋅1=AA + AB = A(1+B) = A \cdot 1 = A, because 1+B=11+B=1: an OR with 1 always equals 1. This is the absorption law. How to read it: if AA alone already makes the expression true, adding a more demanding case (AA AND BB) adds nothing. Pitfall: writing A+AB=A+BA+AB = A+B by “simplifying” as in ordinary algebra.

b)

Expand: (A+B)(A+B‾)=AA+AB‾+BA+BB‾(A+B)(A+\overline{B}) = AA + A\overline{B} + BA + B\overline{B}. Now AA=AAA=A (idempotence) and BB‾=0B\overline{B}=0 (a variable and its negation are never true together), so what remains is A+AB‾+AB=A(1+B‾+B)=AA + A\overline{B} + AB = A(1+\overline{B}+B) = A. You can also apply directly the distributive law of OR over AND: (A+B)(A+B‾)=A+BB‾=A+0=A(A+B)(A+\overline{B}) = A + B\overline{B} = A+0 = A. This second distributive law, X+YZ=(X+Y)(X+Z)X+YZ = (X+Y)(X+Z), has no counterpart with real numbers: it is the one people forget.

c)

De Morgan gives AB‾=A‾+B‾\overline{AB} = \overline{A}+\overline{B}, so AB‾+A‾=A‾+B‾+A‾=A‾+B‾\overline{AB}+\overline{A} = \overline{A}+\overline{B}+\overline{A} = \overline{A}+\overline{B} by idempotence, which is again AB‾\overline{AB}. The expression was already AB‾\overline{AB} from the start: the term A‾\overline{A} was absorbed.

d)

De Morgan on the first factor: A‾+B‾‾=A‾‾⋅B‾‾=AB\overline{\overline{A}+\overline{B}} = \overline{\overline{A}} \cdot \overline{\overline{B}} = AB. The expression becomes AB(A+B)=ABA+ABB=AB+AB=ABAB(A+B) = ABA + ABB = AB + AB = AB. Result: ABAB. The long bar is lifted in one go and changes the connective; the short bar over each letter stays.

e)

A⊕A=0A \oplus A = 0: two identical values give 0. A⊕A‾=1A \oplus \overline{A} = 1: they always differ. A⊕0=AA \oplus 0 = A: XOR with 0 lets the value through. These three identities are the basis of XOR encryption and of swapping two variables without a temporary variable.

f)

Table of (A+B)(A+B‾)(A+B)(A+\overline{B}) on the rows (A,B)=(1,1),(1,0),(0,1),(0,0)(A,B)=(1,1),(1,0),(0,1),(0,0): A+BA+B is 1,1,1,01,1,1,0; A+B‾A+\overline{B} is 1,1,0,11,1,0,1; their product is 1,1,0,01,1,0,0. That is exactly the column of AA. The table confirms the simplification, and it is the only check that cannot be argued with.

Exercise 3: Implication: contrapositive, converse, negation

Consider the statement PP: “if a program compiles, then it contains no syntax error”. Let cc be “the program compiles” and ss be “the program contains a syntax error”, so that PP is written c⇒¬sc \Rightarrow \lnot s.

  • a) Write the contrapositive of PP in symbols, then in words.
  • b) Write the converse of PP in symbols, then in words. Is it true?
  • c) Write the negation of PP. Careful: it is not an implication.
  • d) Prove with a truth table that p⇒qp \Rightarrow q and ¬p∨q\lnot p \lor q are equivalent.
  • e) Deduce a way of writing ¬(p⇒q)\lnot(p \Rightarrow q) without an implication.
  • f) An implication whose premise is false is said to be “vacuously” true. Give an example taken from code.

Type your answers, the page tells you right or wrong 0/9

a)
b)
c)
d)
e)
f)
Show the solution

Answers

  • a) s⇒¬cs \Rightarrow \lnot c: if the program has a syntax error, it does not compile
  • b) ¬s⇒c\lnot s \Rightarrow c, and it is FALSE: type error, missing library, file not found
  • c) ¬P=c∧s\lnot P = c \land s: the program compiles AND contains a syntax error
  • d) Both columns are 1,0,1,11,0,1,1: the expressions are equivalent
  • e) ¬(p⇒q)=p∧¬q\lnot(p \Rightarrow q) = p \land \lnot q
  • f) A loop over an empty array: “for every iteration, the counter increases” is vacuously true and proves nothing
a)

Contrapositive: s⇒¬cs \Rightarrow \lnot c, that is “if the program contains a syntax error, then it does not compile”. The contrapositive swaps AND negates both sides. It always has the same truth value as the original implication: the two have the same table. That is what justifies proof by contrapositive.

b)

Converse: ¬s⇒c\lnot s \Rightarrow c, that is “if the program contains no syntax error, then it compiles”. It is false: a syntactically correct program can fail to compile because of a type error, a missing library or a file that cannot be found. That is a counterexample, and one is enough. The converse has no reason to share the truth value of the implication: confusing the two is the most frequent reasoning error of the course.

c)

To negate an implication is to assert the premise and negate the conclusion: ¬P=c∧s\lnot P = c \land s, that is “the program compiles AND it contains a syntax error”. The negation of an implication is never an implication, and certainly not c⇒sc \Rightarrow s. The landmark: a promise is refuted only by exhibiting the case where it is broken.

d)

Table on the rows (p,q)=(1,1),(1,0),(0,1),(0,0)(p,q)=(1,1),(1,0),(0,1),(0,0). p⇒qp \Rightarrow q is 1,0,1,11,0,1,1. For ¬p∨q\lnot p \lor q: ¬p\lnot p is 0,0,1,10,0,1,1, so ¬p∨q\lnot p \lor q is 1,0,1,11,0,1,1. The two columns agree on all four rows, so the expressions are equivalent. This identity is how all of logic enters circuits: an implication is wired with one NOT and one OR.

e)

¬(p⇒q)=¬(¬p∨q)=p∧¬q\lnot(p \Rightarrow q) = \lnot(\lnot p \lor q) = p \land \lnot q by De Morgan. We recover the answer to question c) by calculation, which confirms it.

f)

Example: in a loop over an empty array, the statement “for every iteration performed, the counter increases” is vacuously true, since no iteration is performed. A unit test run on an empty array therefore always passes if its condition is about the elements: it proves nothing. That is why a specification of the form “if the array contains a duplicate, then the function returns true” must be tested on a case where the premise is actually true.

Exercise 4: Quantifiers and negating a quantified statement

An array TT holds nn integers, indexed from 0 to n−1n-1. We write T[i]T[i] for its element at index ii. The quantifiers ∀\forall (for all) and ∃\exists (there exists) turn a property of one element into a property of the whole array.

  • a) Translate into symbols: “all the elements of TT are strictly positive”.
  • b) Write the negation of this statement, in symbols and in words.
  • c) Translate: “TT contains at least one duplicate”, then negate this statement.
  • d) Are the statements ∀x∈Z, ∃y∈Z, x+y=0\forall x \in \mathbb{Z},\ \exists y \in \mathbb{Z},\ x+y=0 and ∃y∈Z, ∀x∈Z, x+y=0\exists y \in \mathbb{Z},\ \forall x \in \mathbb{Z},\ x+y=0 true? What does the comparison show?
  • e) On an empty array, is the statement of question a) true or false?
  • f) How many comparisons are needed, in the worst case, to verify the statement of question a)? And to refute it?

Type your answers, the page tells you right or wrong 0/12

a)
b)
c)
d)
e)
f)
Show the solution

Answers

  • a) ∀i∈{0,…,n−1}, T[i]>0\forall i \in \{0,\dots,n-1\},\ T[i] > 0
  • b) ∃i∈{0,…,n−1}, T[i]≤0\exists i \in \{0,\dots,n-1\},\ T[i] \le 0: at least one element is negative or zero
  • c) ∃i, ∃j, (i≠j)∧(T[i]=T[j])\exists i,\ \exists j,\ (i \neq j) \land (T[i]=T[j]), whose negation is ∀i, ∀j, (i≠j)⇒(T[i]≠T[j])\forall i,\ \forall j,\ (i \neq j) \Rightarrow (T[i] \neq T[j])
  • d) The first is true (take y=−xy=-x), the second false: the order of the quantifiers is not decoration
  • e) Vacuously true
  • f) nn comparisons to verify, a single one at best to refute
a)

∀i∈{0,…,n−1}, T[i]>0\forall i \in \{0,\dots,n-1\},\ T[i] > 0. We quantify over the indices, not over the values: this is the form that translates directly into a loop.

b)

Negation swaps the quantifier and negates the property: ∃i∈{0,…,n−1}, T[i]≤0\exists i \in \{0,\dots,n-1\},\ T[i] \le 0, that is “there is at least one element of TT that is negative or zero”. In words, the negation of “all” is not “none” but “at least one is not”. Confusing the two is the costliest error of the chapter, because it goes unnoticed when you reread.

c)

“TT contains at least one duplicate” is written ∃i, ∃j, (i≠j)∧(T[i]=T[j])\exists i,\ \exists j,\ (i \neq j) \land (T[i]=T[j]). Its negation is ∀i, ∀j, (i≠j)⇒(T[i]≠T[j])\forall i,\ \forall j,\ (i \neq j) \Rightarrow (T[i] \neq T[j]): all the elements are pairwise distinct. Notice that the negation of an ∃\exists followed by an AND gives a ∀\forall followed by an implication, not by an AND.

d)

The first is true: for each xx, choose y=−xy=-x, and this choice is allowed to depend on xx since yy is quantified after it. The second is false: it would require a single yy that cancels every xx at once, but yy would work for x=−yx=-y and fail for x=−y+1x=-y+1. The comparison shows that the order of the quantifiers is not decoration: ∀∃\forall\exists and ∃∀\exists\forall are two different statements, and one can be true while the other is false.

e)

It is true. On an empty array there is no index ii, so no opportunity to find a counterexample: the statement is vacuously true. A function that scans the array and returns true if no violation was found therefore does return true on the empty array, which is the correct behaviour and not a bug.

f)

To verify the statement, you must go all the way: nn comparisons in the worst case, and exactly nn when the statement is true. To refute it, a single counterexample is enough, so 1 comparison at best, and nn at worst if the only offending element is the last one. This asymmetry is that of ∀\forall: expensive to confirm, sometimes immediate to disprove. For an ∃\exists, it is the other way around.

Exercise 5: From the truth table to the expression: normal forms

A Boolean function f(A,B,C)f(A,B,C) is given by its truth table. Every function has a canonical sum-of-products form: keep the rows where ff equals 1, write for each one the product of the three variables (complemented when they equal 0), and join these products with ORs.

AABBCCff
0000
0010
0100
0111
1000
1011
1101
1111
  • a) Write the canonical sum-of-products form of ff.
  • b) How many AND, OR and NOT gates would be needed to wire this form as it stands?
  • c) Show that f=AB+BC+ACf = AB + BC + AC.
  • d) Describe in one plain-English sentence what ff computes.
  • e) Write the canonical form of f‾\overline{f} and check that it does have 4 terms.

Type your answers, the page tells you right or wrong 0/12

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) f=A‾BC+AB‾C+ABC‾+ABCf = \overline{A}BC + A\overline{B}C + AB\overline{C} + ABC
  • b) 8 AND, 3 NOT and 3 OR, so 14 gates
  • c) f=AB+BC+ACf = AB+BC+AC: same four rows, and 5 gates instead of 14
  • d) The majority function: true as soon as two of the three variables equal 1
  • e) f‾=A‾ B‾ C‾+A‾ B‾C+A‾BC‾+AB‾ C‾\overline{f} = \overline{A}\,\overline{B}\,\overline{C} + \overline{A}\,\overline{B}C + \overline{A}B\overline{C} + A\overline{B}\,\overline{C}, 4 terms, and 4+4=84+4=8 rows
a)

The rows where f=1f=1 are (0,1,1)(0,1,1), (1,0,1)(1,0,1), (1,1,0)(1,1,0) and (1,1,1)(1,1,1). Each row gives a product: A‾BC\overline{A}BC, AB‾CA\overline{B}C, ABC‾AB\overline{C} and ABCABC. Hence f=A‾BC+AB‾C+ABC‾+ABCf = \overline{A}BC + A\overline{B}C + AB\overline{C} + ABC. The rule: a variable that equals 1 on the row enters as it is, a variable that equals 0 enters complemented. Each product equals 1 on its own row and on that row alone, so the sum equals 1 on exactly the four rows we want.

b)

Each product of three variables requires 2 two-input AND gates, so 8 AND gates, plus 3 NOT gates (one per complemented variable, here A‾\overline{A}, B‾\overline{B}, C‾\overline{C}), plus 3 OR gates to join the 4 terms. Total: 14 gates. It is precisely this cost that motivates simplification.

c)

Expand the proposed form. ABAB equals 1 on the rows (1,1,0)(1,1,0) and (1,1,1)(1,1,1); BCBC equals 1 on (0,1,1)(0,1,1) and (1,1,1)(1,1,1); ACAC equals 1 on (1,0,1)(1,0,1) and (1,1,1)(1,1,1). The union of these rows is {(0,1,1),(1,0,1),(1,1,0),(1,1,1)}\{(0,1,1),(1,0,1),(1,1,0),(1,1,1)\}, exactly the set of rows where f=1f=1, and no other row is reached. The tables agree, so f=AB+BC+ACf = AB+BC+AC. Algebraically: ABCABC is covered by all three terms and can be duplicated by idempotence, which lets you merge A‾BC+ABC=BC\overline{A}BC + ABC = BC, AB‾C+ABC=ACA\overline{B}C+ABC = AC and ABC‾+ABC=ABAB\overline{C}+ABC = AB. We go from 14 gates to 5.

d)

ff equals 1 exactly when at least two of the three variables equal 1: it is the majority function. It is used for majority voting in redundant systems (triple modular redundancy), where the failure of one sensor out of three is tolerated.

e)

f‾\overline{f} equals 1 on the four remaining rows: (0,0,0)(0,0,0), (0,0,1)(0,0,1), (0,1,0)(0,1,0) and (1,0,0)(1,0,0). Hence f‾=A‾ B‾ C‾+A‾ B‾C+A‾BC‾+AB‾ C‾\overline{f} = \overline{A}\,\overline{B}\,\overline{C} + \overline{A}\,\overline{B}C + \overline{A}B\overline{C} + A\overline{B}\,\overline{C}, which does have 4 terms. The 8 rows are split between ff and f‾\overline{f} with nothing left over: a free check to do every time.

Part B: problems and reasoning (/50)

Exercise 6: Four-variable Karnaugh map

A Karnaugh map arranges the 24=162^{4}=16 rows of a four-variable table in a grid where two adjacent cells differ in a single variable. Grouping two adjacent cells eliminates that variable; a group of 4 eliminates 2, a group of 8 eliminates 3. Groups must be rectangles whose number of cells is a power of 2, and opposite edges of the map are adjacent.

Consider f(A,B,C,D)f(A,B,C,D), which equals 1 on the combinations whose decimal number 8A+4B+2C+D8A+4B+2C+D belongs to {0,2,5,7,8,10,13,15}\{0, 2, 5, 7, 8, 10, 13, 15\}, and 0 elsewhere. The map below was filled in by a student.

AB\CDAB \backslash CD0000010111111010
00001011
01010110
11110110
10101011
  • a) Check that the cell in row AB=01AB=01 and column CD=11CD=11 does correspond to a minterm in the list.
  • b) The map contains errors: some cells hold a 1 whose number is not in the list. Find them and correct them.
  • c) On the corrected map, form the largest possible groups and give the simplified expression of ff.
  • d) Identify the function obtained and write it with a single connective.
  • e) Compare the number of gates in the canonical form and in the simplified form.

Type your answers, the page tells you right or wrong 0/11

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) 8(0)+4(1)+2(1)+1=78(0)+4(1)+2(1)+1 = 7, which is indeed in the list
  • b) Numbers 3 and 11 in column CD=11CD=11 are faulty: these two cells equal 0, and 8 cells holding 1 remain
  • c) f=B‾ D‾+BDf = \overline{B}\,\overline{D} + BD: the four corners and the central block, AA and CC disappear
  • d) f=B⊕D‾f = \overline{B \oplus D}, the equality comparator
  • e) About 35 gates against 5, a ratio of roughly 7 to 1
a)

The row AB=01AB=01 means A=0A=0 and B=1B=1; the column CD=11CD=11 means C=1C=1 and D=1D=1. The number is 8(0)+4(1)+2(1)+1=78(0)+4(1)+2(1)+1 = 7, which is indeed in the list. This check is worth doing on two or three cells before grouping anything: a badly filled map produces an impeccable simplification of the wrong function.

b)

Test the column CD=11CD=11, which is all 1s. Its four numbers are 33 (row 00), 77 (row 01), 1515 (row 11) and 1111 (row 10). But 33 and 1111 are not in the list {0,2,5,7,8,10,13,15}\{0,2,5,7,8,10,13,15\}. The faulty cells are therefore those of rows AB=00AB=00 and AB=10AB=10 in column CD=11CD=11: they equal 0. After correcting them, the map becomes: row 0000: 1,0,0,11,0,0,1; row 0101: 0,1,1,00,1,1,0; row 1111: 0,1,1,00,1,1,0; row 1010: 1,0,0,11,0,0,1. There are then 8 cells holding 1, matching the 8 minterms announced. This count is the check not to skip.

c)

First group: the four corners of the map, cells (AB,CD)=(00,00),(00,10),(10,00),(10,10)(AB,CD) = (00,00), (00,10), (10,00), (10,10), that is the numbers 0,2,8,100, 2, 8, 10. Since opposite edges are adjacent, these four corners do form a rectangle of 4. Over this group AA changes and CC changes, but B=0B=0 and D=0D=0 throughout: it gives B‾ D‾\overline{B}\,\overline{D}. Second group: the central block of rows 0101 and 1111 over columns 0101 and 1111, that is the numbers 5,7,13,155, 7, 13, 15. AA changes, CC changes, but B=1B=1 and D=1D=1: it gives BDBD. All 8 cells are covered, so f=B‾ D‾+BDf = \overline{B}\,\overline{D} + BD. The variables AA and CC have disappeared: the function does not depend on them.

d)

ff equals 1 exactly when BB and DD have the same value: it is the exclusive NOR (XNOR), f=B⊕D‾f = \overline{B \oplus D}, called an equality comparator. It is the building block of a binary word comparator: test equality bit by bit, then join the results with ANDs.

e)

Canonical form: 8 minterms of 4 variables, so 24 two-input AND gates, 7 OR gates and 4 NOT gates, about 35 gates. Simplified form: 2 NOT gates, 2 AND gates, 1 OR gate, so 5 gates, or a single XNOR gate if the library offers one. The ratio is roughly 7 to 1, and it is paid for in silicon area, power consumption and propagation delay.

AB \ CD00011110001001010110110110101001

Exercise 7: Logic circuits: half adder and NAND gate

The circuit below takes two bits AA and BB and produces two outputs SS and CC. The two dots mark junctions: the same signal feeds both gates.

XORANDABSC
  • a) Give the Boolean expressions of SS and CC in terms of AA and BB.
  • b) Draw up the table of the four cases and interpret SS and CC as the binary sum of AA and BB.
  • c) A full adder also receives a carry-in CinC_{\text{in}}. Give the expressions of its sum SS and of its carry-out CoutC_{\text{out}}.
  • d) Show that the NAND gate, defined by A↑B=AB‾A \uparrow B = \overline{AB}, can be used to build NOT, AND and OR. How many NAND gates does each one require?
  • e) Why does chip manufacturing favour a universal gate?

Type your answers, the page tells you right or wrong 0/14

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) S=A⊕BS = A \oplus B and C=ABC = AB
  • b) SS is 0,1,1,00,1,1,0 and CC is 0,0,0,10,0,0,1: the pair (C,S)(C,S) gives 0,1,1,20,1,1,2, that is A+BA+B
  • c) S=A⊕B⊕CinS = A \oplus B \oplus C_{\text{in}} and Cout=AB+Cin(A⊕B)C_{\text{out}} = AB + C_{\text{in}}(A \oplus B)
  • d) 1 NAND for NOT, 2 for AND, 3 for OR: the gate is universal
  • e) 4 transistors against 6: a single pattern etched, tested and reused everywhere
a)

AA and BB both enter the XOR gate at the top and the AND gate at the bottom. So S=A⊕BS = A \oplus B and C=ABC = AB. The dots on the wires mark junctions: the same signal feeds two gates, which is allowed and costs nothing more than a trace.

b)

Rows (A,B)=(0,0),(0,1),(1,0),(1,1)(A,B) = (0,0), (0,1), (1,0), (1,1). SS is 0,1,1,00, 1, 1, 0 and CC is 0,0,0,10, 0, 0, 1. Reading CC as the most significant bit and SS as the least significant bit, the pair (C,S)(C,S) is 00,01,01,1000, 01, 01, 10 in binary, that is 0,1,1,20, 1, 1, 2 in decimal: exactly A+BA+B. The circuit adds. The case 1+11+1 is what justifies the carry: the sum is 2, which does not fit in one bit.

c)

S=A⊕B⊕CinS = A \oplus B \oplus C_{\text{in}}: the sum equals 1 when an odd number of inputs equal 1. Cout=AB+Cin(A⊕B)C_{\text{out}} = AB + C_{\text{in}}(A \oplus B): there is a carry either because AA and BB both equal 1, or because exactly one of them equals 1 and the carry-in is added to it. Numerical check: for every combination, 2Cout+S=A+B+Cin2C_{\text{out}} + S = A+B+C_{\text{in}}, which equals 0, 1, 2 or 3. This block is wired nn times in a chain to add two nn-bit words.

d)

NOT: A‾=A↑A\overline{A} = A \uparrow A, a single NAND, with both inputs tied together. AND: AB=AB‾‾=(A↑B)↑(A↑B)AB = \overline{\overline{AB}} = (A \uparrow B) \uparrow (A \uparrow B), so 2 NAND gates. OR: by De Morgan, A+B=A‾⋅B‾‾=A‾↑B‾=(A↑A)↑(B↑B)A+B = \overline{\overline{A} \cdot \overline{B}} = \overline{A} \uparrow \overline{B} = (A \uparrow A) \uparrow (B \uparrow B), so 3 NAND gates. Since every circuit can be described with NOT, AND and OR, every circuit can be described with NAND gates alone: the gate is called universal. The NOR gate is universal too.

e)

Because a plant that etches a single transistor pattern etches it better, tests it with a single test bench and reuses the same library cell everywhere. In CMOS technology, the NAND is also the cheapest gate: 4 transistors, against 6 for an AND, which is just a NAND followed by an inverter. A circuit built entirely of NAND gates is therefore often smaller than the same circuit written with ANDs and ORs, even if it has more gates on the schematic.

NANDNANDNANDABA + B

Exercise 8: Five statements to correct

Each of the following five statements is FALSE. Say why and give the correct statement.

  • 1) “If an implication is true, then its converse is true.”
  • 2) “AB‾=A‾⋅B‾\overline{AB} = \overline{A} \cdot \overline{B}.”
  • 3) “A truth table with four propositions has eight rows.”
  • 4) “A+BC=(A+B)CA + BC = (A+B)C.”
  • 5) “Logical OR is exclusive: when A=1A=1 and B=1B=1, A+BA+B equals 0.”

Type your answers, the page tells you right or wrong 0/9

1)
2)
3)
4)
5)
Show the solution

Answers

  • 1) FALSE: it is the CONTRAPOSITIVE that shares the truth value, not the converse
  • 2) FALSE: AB‾=A‾+B‾\overline{AB} = \overline{A} + \overline{B}, lifting the bar changes the connective
  • 3) FALSE: 24=162^{4} = 16 rows; eight rows correspond to three propositions
  • 4) FALSE: A+BC=(A+B)(A+C)A + BC = (A+B)(A+C)
  • 5) FALSE: logical OR is inclusive, 1+1=11+1 = 1. The exclusive connective is A⊕B=AB‾+A‾BA \oplus B = A\overline{B}+\overline{A}B
1)

FALSE. An implication and its converse are two independent statements. “If x=2x=2 then x2=4x^{2}=4” is true, but its converse “if x2=4x^{2}=4 then x=2x=2” is false for x=−2x=-2. Correct statement: an implication always has the same truth value as its CONTRAPOSITIVE, never necessarily as its converse.

2)

FALSE. This is De Morgan's law applied wrongly: lifting the bar also changes the connective. Counterexample with A=1A=1, B=0B=0: AB‾=0‾=1\overline{AB} = \overline{0} = 1, whereas A‾⋅B‾=0⋅1=0\overline{A} \cdot \overline{B} = 0 \cdot 1 = 0. Correct statement: AB‾=A‾+B‾\overline{AB} = \overline{A} + \overline{B}, and symmetrically A+B‾=A‾⋅B‾\overline{A+B} = \overline{A} \cdot \overline{B}.

3)

FALSE. Each proposition doubles the number of rows: there are 2n2^{n} of them, so 24=162^{4} = 16 for four propositions. Eight rows correspond to three propositions. Correct statement: a table with nn propositions has 2n2^{n} rows, and the number of Boolean functions of nn variables is 22n2^{2^{n}}.

4)

FALSE. OR distributes over AND by duplicating the term: counterexample with A=1A=1, B=0B=0, C=0C=0: on the left 1+0=11 + 0 = 1, on the right (1+0)⋅0=0(1+0) \cdot 0 = 0. Correct statement: A+BC=(A+B)(A+C)A + BC = (A+B)(A+C).

5)

FALSE. Logical OR is inclusive: 1+1=11+1 = 1. It is true as soon as at least one input equals 1, including when both equal 1. Correct statement: the connective that equals 0 when both inputs equal 1 is the exclusive OR, written A⊕BA \oplus B, and it satisfies A⊕B=AB‾+A‾BA \oplus B = A\overline{B} + \overline{A}B.

Exercise 9: Problem: access control for a server room

Access to a server room is controlled by four binary signals: BB (the badge is valid), CC (the code entered is correct), HH (it is within business hours) and DD (the manager has granted a dispensation). The specification says: the door opens if the badge is valid AND the code is correct AND it is within business hours; it also opens if the badge is valid AND the code is correct AND a dispensation has been granted.

  • a) Write the Boolean expression AA for opening the door, directly from the specification.
  • b) Simplify AA and name the law used.
  • c) Count the two-input gates before and after simplification.
  • d) Out of the 16 possible combinations, how many open the door?
  • e) Write the REFUSAL condition A‾\overline{A} as a sum, and interpret each term.
  • f) An intern proposes A′=BC+H+DA' = BC + H + D. Give a combination showing that this version is dangerous.

Type your answers, the page tells you right or wrong 0/12

a)
b)
c)
d)
e)
f)
Show the solution

Answers

  • a) A=BCH+BCDA = BCH + BCD
  • b) A=BC(H+D)A = BC(H+D), by factoring
  • c) 5 gates before, 3 after
  • d) 3 combinations out of 16
  • e) A‾=B‾+C‾+H‾ D‾\overline{A} = \overline{B} + \overline{C} + \overline{H}\,\overline{D}: invalid badge, or wrong code, or outside business hours AND without a dispensation
  • f) B=0B=0, C=0C=0, H=1H=1, D=0D=0: the real system gives 0, the intern's version gives 1, and the door opens with no badge or code
a)

The specification translates term by term: A=BCH+BCDA = BCH + BCD. Each “AND” becomes a product, and “also” becomes an OR.

b)

BCH+BCD=BC(H+D)BCH + BCD = BC(H+D) by factoring, that is the distributive law of AND over OR read from right to left. Business reading: you need the badge AND the code, plus at least one of the two time justifications. This form states the specification better than the first, because it separates identification from time authorization.

c)

Before: each product of three variables requires 2 AND gates, so 4 AND gates, plus 1 OR gate: 5 gates. After: 1 OR gate for H+DH+D, then 2 AND gates for B⋅C⋅(H+D)B \cdot C \cdot (H+D): 3 gates. We save 2 gates out of 5, and above all one level of logic, hence delay.

d)

AA equals 1 when B=1B=1, C=1C=1 and H+D=1H+D=1. With BB and CC fixed, the 4 combinations of (H,D)(H,D) remain, 3 of which satisfy H+D=1H+D=1: (1,0)(1,0), (0,1)(0,1) and (1,1)(1,1). So 3 combinations out of 16 open the door. The proportion is a useful check: a security system that opened on half of the combinations would be suspect by design.

e)

A‾=BC(H+D)‾=BC‾+H+D‾=B‾+C‾+H‾ D‾\overline{A} = \overline{BC(H+D)} = \overline{BC} + \overline{H+D} = \overline{B} + \overline{C} + \overline{H}\,\overline{D}, by De Morgan applied twice. Three causes of refusal, and they are clearly distinct: invalid badge, or wrong code, or outside business hours AND without a dispensation. Notice that the third cause is a product, not a sum: being outside business hours is not enough to refuse if a dispensation exists.

f)

Take B=0B=0, C=0C=0, H=1H=1, D=0D=0: the real system gives A=0⋅0⋅1=0A = 0 \cdot 0 \cdot 1 = 0, the door stays closed. The intern's version gives A′=0+1+0=1A' = 0 + 1 + 0 = 1: the door opens for anyone, with no badge or code, simply because it is within business hours. The error comes from operator precedence: the intern wrote an OR where the specification requires BCBC to be a mandatory common factor. In security logic, a necessary condition is factored out, never added.

ORANDHDCBA

Exercise 10: Problem: checking an argument with logic

An argument is valid when the conjunction of its premises implies its conclusion for ALL possible truth values, that is, when the corresponding implication is a tautology. Validity says nothing about the content: it says only that the conclusion cannot be false when the premises are true.

  • a) Formalize: “if the server is down, the site is unreachable; but the site is reachable; therefore the server is not down”. Is this argument valid?
  • b) Formalize: “if the server is down, the site is unreachable; but the site is unreachable; therefore the server is down”. Is this argument valid?
  • c) Prove with a truth table that ((p⇒q)∧¬q)⇒¬p\big((p \Rightarrow q) \land \lnot q\big) \Rightarrow \lnot p is a tautology.
  • d) Can the argument of question b) have true premises and a true conclusion? Does that make it valid?
  • e) Study the validity of: “if pp then qq; if qq then rr; therefore if pp then rr”.
  • f) Why does this exercise matter to a programmer reading an error message?

Type your answers, the page tells you right or wrong 0/12

a)
b)
c)
d)
e)
f)
Show the solution

Answers

  • a) ((p⇒q)∧¬q)⇒¬p\big((p \Rightarrow q) \land \lnot q\big) \Rightarrow \lnot p: valid, it is modus tollens
  • b) ((p⇒q)∧q)⇒p\big((p \Rightarrow q) \land q\big) \Rightarrow p: invalid, affirming the consequent, counterexample p=0p=0, q=1q=1
  • c) The antecedent is 0,0,0,10,0,0,1 and the final implication 1,1,1,11,1,1,1: it is a tautology
  • d) Yes with p=1p=1, q=1q=1, and that does NOT make it valid
  • e) Valid: the hypothetical syllogism, no counterexample on the 8 rows
  • f) An error message gives a consequence, not a cause: look for the premises you can disprove
a)

Let pp be “the server is down” and qq be “the site is unreachable”. The argument is ((p⇒q)∧¬q)⇒¬p\big((p \Rightarrow q) \land \lnot q\big) \Rightarrow \lnot p. It is valid: this is modus tollens. Look for the rows where both premises are true: ¬q\lnot q true forces q=0q=0; then p⇒qp \Rightarrow q is true only if p=0p=0. On that single row, the conclusion ¬p\lnot p equals 1. No row makes the premises true and the conclusion false, so the argument is valid.

b)

The argument is ((p⇒q)∧q)⇒p\big((p \Rightarrow q) \land q\big) \Rightarrow p. It is not valid. Counterexample: p=0p=0, q=1q=1. Then p⇒qp \Rightarrow q equals 1 and qq equals 1, both premises are true, but the conclusion pp is false. This is the fallacy of affirming the consequent: the site is unreachable, but perhaps because of the network, the DNS or an expired certificate, and not because the server is down.

c)

Table on the four rows (p,q)=(1,1),(1,0),(0,1),(0,0)(p,q) = (1,1), (1,0), (0,1), (0,0). p⇒qp \Rightarrow q is 1,0,1,11, 0, 1, 1. ¬q\lnot q is 0,1,0,10, 1, 0, 1. The conjunction (p⇒q)∧¬q(p \Rightarrow q) \land \lnot q is therefore 0,0,0,10, 0, 0, 1: it is true only on the last row. On that row, ¬p\lnot p equals 1. The final implication is therefore 1,1,1,11, 1, 1, 1: it is true on all four rows, so it is a tautology. Notice that it was enough to examine a single row, the one where the antecedent is true; on the others, the implication is vacuously true.

d)

Yes: with p=1p=1 and q=1q=1, both premises are true and so is the conclusion. That does not make the argument valid, though. Validity requires the conclusion to be true on ALL the rows where the premises are, and the row p=0p=0, q=1q=1 is enough to ruin that requirement. An invalid argument can give a correct conclusion by accident: that is what makes it dangerous, because it seems to work until the day it does not.

e)

This is the hypothetical syllogism, ((p⇒q)∧(q⇒r))⇒(p⇒r)\big((p \Rightarrow q) \land (q \Rightarrow r)\big) \Rightarrow (p \Rightarrow r), and it is valid. Over the 8 rows, look for a counterexample: it would need p=1p=1, r=0r=0 and both premises true. From p=1p=1 and p⇒qp \Rightarrow q true we get q=1q=1; from q=1q=1 and q⇒rq \Rightarrow r true we get r=1r=1, which contradicts r=0r=0. No counterexample exists, so the argument is valid. This is the principle of chaining implications, the one that allows lemmas to be stacked in a proof.

f)

An error message gives an observed consequence, not a cause. “The page does not load” is the qq of question b): concluding “the server is down” from it is affirming the consequent. The correct argument is modus tollens: “if the server is running, the test page responds; but the test page does not respond, therefore the server is not running”. Good debugging means looking for the premises you can disprove, not the ones you would like to confirm.

Something missing from this set? A type of exercise your teacher gives, a problem you are stuck on, a mistake you spotted: tell me, I read every request.

Sending goes through your free space: that is how I can answer you and tell you when the exercise is added. An email is enough, no password. Create your space. Rather not create an account? Write to me directly.

What is it about?
I read every request myself, nobody else sees it. No need to write your name or your school.
When your request is dealt with, you hear about it by email and in your space.

By sending, you agree that I read your request to answer it. It is kept with your account, erased with it, and never published: privacy policy.

Next chapter Proof techniques, 201-N11

© Ahmed Squalli Houssaini. Exercise set published at www.letuteurscientifique.ca/en/exercices/cegep-n11-boolean-logic. Free for personal and classroom use; republishing it elsewhere requires written permission (legal notice).

See also

Looking for a math tutor for computer science in Montreal?

Get in touch for a first session. With a B.Sc. in computer science from McGill and an M.Sc. in applied computer science from Concordia, I teach Boolean logic the way you will use it next: in the conditions of a program, in database queries and in circuits.

Site by Studio Squalli