Professional Documents
Culture Documents
1
Administratrivia
• HW 1: due next Friday
– Section 1.1 # 49, 52
– Section 1.2 # 34, 44, 46
2
Applications of Boolean logic
• Computer programs
• And computer addition
• Logic problems
• Sudoku
3
Boolean propositions
• A proposition is a statement that can be
either true or false
– “The sky is blue”
– “I is a Engineering major”
– “x == y”
• Not propositions:
– “Are you Bob?”
– “x := 7”
4
Boolean variables
• We use Boolean variables to refer to
propositions
– Usually are lower case letters starting with p
(i.e. p, q, r, s, etc.)
– A Boolean variable can have one of two
values true (T) or false (F)
• A proposition can be…
– A single variable: p
– An operation of multiple variables: p(qr)
5
Introduction to Logical Operators
• About a dozen logical operators
– Similar to algebraic operators + * - /
• In the following examples,
– p = “Today is Friday”
– q = “Today is my birthday”
6
Logical operators: Not
• A not operation switches (negates) the truth value
• Symbol: or ~ p p
• In C++ and Java, T F
the operand is ! F T
p = “Today is not Friday”
7
Logical operators: And
• An and operation is true if both operands are true
• Symbol:
p q pq
– It’s like the ‘A’ in And
T T T
• In C++ and Java,
T F F
the operand is &&
F T F
• pq = “Today is Friday and
F F F
today is my birthday”
8
Logical operators: Or
• An or operation is true if either operands are true
• Symbol: p q pq
• In C++ and Java, T T T
the operand is || T F T
F T T
• pq = “Today is Friday or
F F F
today is my birthday (or
possibly both)”
9
Logical operators: Exclusive Or
• An exclusive or operation is true if one of the operands
are true, but false if both are true
• Symbol: p q pq
• Often called XOR T T F
• pq (p q) ¬(p q) T F T
• In Java, the operand is ^ F T T
(but not in C++) F F F
• pq = “Today is Friday or today
is my birthday, but not both”
10
Inclusive Or versus Exclusive
Or
• Do these sentences mean inclusive or
exclusive or?
– Experience with C++ or Java is required
– Lunch includes soup or salad
– To enter the country, you need a passport or
a driver’s license
– Publish or perish
11
Logical operators: Nand and Nor
• The negation of And and Or, respectively
• Symbols: | and ↓, respectively
– Nand: p|q ¬(pq)
– Nor: p↓q ¬(pq)
13
Logical operators: Conditional 2
• Let p = “I am elected” and q = “I will lower taxes”
• I state: p q = “If I
p q pq
am elected, then I
T T T
will lower taxes”
T F F
• Consider all
F T T
possibilities
F F T
• Note that if p is false, then
the conditional is true regardless of whether q is
true or false
14
Logical operators: Conditional 3
15
Logical operators: Conditional 4
• Alternate ways of stating a conditional:
– p implies q
– If p, q
– p is sufficient for q
– q if p
– q whenever p
– q is necessary for p
– p only if q I don’t like this one
16
Logical operators: Bi-conditional 1
• A bi-conditional means “p if and only if q”
• Symbol:
p q pq
• Alternatively, it means
“(if p then q) and T T T
(if q then p)” T F F
• pq pq qp F T F
• Note that a bi-conditional F F T
has the opposite truth values
of the exclusive or
17
Logical operators: Bi-conditional 2
• Let p = “You take this class” and q = “You get a
grade”
• Then pq means p q pq
“You take this class if T T T
and only if you get a T F F
grade” F T F
• Alternatively, it means “If F F T
you take this class, then
you get a grade and if you get a grade then you
take (took) this class”
18
Boolean operators summary
p q result conclusion
T T T T
T F ? F
F T ? T
F F T T 24
Translation Example 2
• “I have neither given nor received help on this
exam”
– Rephrased: “I have not given nor received …”
– Let p = “I have given help on this exam”
– Let q = “I have received help on this exam”
• Translation is: pq
p q p pq
T T F F
T F F T
F T T F
25
F F T F
Translation Example 2
• What they mean is “I have not given and I have not
received help on this exam”
– Or “I have not (given nor received) help on this exam”
p q pq (pq)
T T F F
T F F F
F T F F
F F T T
11000
11011
11000
30
&& vs. & in C/C++
Consider the In C/C++, any value other than 0 is true
following:
The binary for the integer 11 is 01011
int p = 11; The binary for the integer 20 is 10100
int q = 20; Notice the double ampersand – this is a Boolean operation
if ( p && q ) {
}
As p and q are both true, this is true
if ( p & q ) {
Notice the single ampersand – this is a bitwise operation
}
34
Logical Equivalences of And
• pT≡p Identity law
p T pT
T T T
F T F
• (p q) r ≡ p (q r) Associative law
p q r pq (pq)r qr p(qr)
T T T T T T T
T T F T F F F
T F T F F F F
T F F F F F F
F T T F F T F
F T F F F F F
F F T F F F F
F F F F F F F
37
Logical Equivalences of Or
• pT≡T Identity law
• pF≡p Domination law
• pp≡p Idempotent law
• pq≡qp Commutative law
• (p q) r ≡ p (q r) Associative law
38
Corollary of the Associative Law
• (p q) r ≡ p q r
• (p q) r ≡ p q r
• Similar to (3+4)+5 = 3+4+5
• Only works if ALL the operators are the same!
39
Logical Equivalences of Not
• ¬(¬p) ≡ p Double negation law
• p ¬p ≡ T Negation law
• p ¬p ≡ F Negation law
40
DeMorgan’s Law
• Probably the most important logical equivalence
• To negate pq (or pq), you “flip” the sign, and
negate BOTH p and q
– Thus, ¬(p q) ≡ ¬p ¬q
– Thus, ¬(p q) ≡ ¬p ¬q
• Absorption
p (p q) ≡ p
p (p q) ≡ p
42
How to prove two propositions
are equivalent?
• Two methods:
– Using truth tables
• Not good for long formulae
• In this course, only allowed if specifically stated!
– Using the logical equivalences
• The preferred method
( p r ) (q r ) ( p q) r
43
Using Truth Tables
( p r ) (q r ) ( p q) r
p q r p→r q →r (p→r)(q →r) pq (pq) →r
T T T T T T T T
T T F F F F T F
T F T T T T F T
T F F F T T F T
F T T T T T F T
F T F T F T F T
F F T T T T F T
F F F T T T F T 44
Using Logical Equivalences
( p r ) (q r ) ( p q) r Original statement
(p Definition r ) ( p pq) qr p q
r ) (qofimplication
(p DeMorgan’s
r ) (q Law
r ) ( p( pqq))
r p q
Associativity
p r q ofrOr (ppr q) (rq r ) p r q r
Re-arranging
p q r r p q r
p q Law
Idempotent r
rp
r rq r
45
Logical Thinking
• At a trial:
– Bill says: “Sue is guilty and Fred is innocent.”
– Sue says: “If Bill is guilty, then so is Fred.”
– Fred says: “I am innocent, but at least one of the
others is guilty.”
• Let b = Bill is innocent, f = Fred is innocent, and
s = Sue is innocent
• Statements are:
– ¬s f
– ¬b → ¬f
– f (¬b ¬s)
46
Can all of their statements be true?
• Show: (¬s f) (¬b → ¬f) (f (¬b ¬s))
b f s ¬b ¬f ¬s ¬sf ¬b→¬f ¬b¬s f(¬b¬s)
T T T F F F F T F F
T T F F F T T T T T
T F T F T F F T F F
T F F F T T F T T F
F T T T F F F F T T
F T F T F T T F T T
F F T T T F F T T F
F F F T T T F T T F 47
Are all of their statements true?
Show values for s, b, and f such
that the equation is true
(s f ) (b f ) ( f (b s )) T Original statement
(s f ) (b f ) ( f (b s )) T Definition of implication
s f (b f ) f (b s ) T Associativity of AND
s f f (b f ) (b s ) T Re-arranging
s f (b f ) (b s ) T Idempotent law
f (b f ) s (s b) T Re-arranging
f (b f ) s T Absorption law
f (b f ) s T Re-arranging
( f b) ( f f ) s T Distributive law
( f b) F s T Negation law
( f b ) s T Domination law
f b s T Associativity of AND
48
What if it weren’t possible to assign
such values to s, b, and f?
(s f ) (b f ) ( f (b s )) s T Original statement
(s f ) (b f ) ( f (b s )) s T Definition of implication
... (same as previous slide)
( f b ) s s T Domination law
f b s s T Re-arranging
f b F T Negation law
f F T Domination law
F T Domination law
Contradiction!
49
Functional completeness
• All the “extended” operators have
equivalences using only the 3 basic
operators (and, or, not)
– The extended operators: nand, nor, xor,
conditional, bi-conditional
• Given a limited set of operators, can you
write an equivalence of the 3 basic
operators?
– If so, then that group of operators is
functionally complete
50
Functional completeness of NAND
• Show that | (NAND) is functionally complete
• Equivalence of NOT:
– p | p ≡ p
(p p) ≡ p Equivalence of NAND
(p) ≡ p Idempotent law
51
Functional completeness of NAND
• Equivalence of AND:
– p q ≡ (p | q) Definition of nand
– p|p How to do a not using nands
– (p | q) | (p | q) Negation of (p | q)
• Equivalence of OR:
– p q ≡ (p q) DeMorgan’s equivalence of OR
– As we can do AND and OR with NANDs, we can thus
do ORs with NANDs