01-intro-and-boolean
01-intro-and-boolean
7
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”
8
• Not propositions:
• “Are you Bob?”
• “x := 7”
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…
9
• A single variable: p
• An operation of multiple variables: p(qr)
Introduction to Logical Operators
• About a dozen logical operators
• Similar to algebraic operators + * - /
• In the following examples,
10
• p = “Today is Friday”
• q = “Today is my birthday”
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”
11
Logical operators: And
• An and operation is true if both operands are true
• Symbol:
• It’s like the ‘A’ in And
p q pq
T T T
• In C++ and Java,
the operand is && T F F
• pq = “Today is Friday and F T F
today is my birthday” F F F
12
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
• pq = “Today is Friday or F T T
today is my birthday (or F F F
possibly both)”
13
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”
14
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
15
• To enter the country, you need a passport or a driver’s license
• Publish or perish
Logical operators: Nand and Nor
• The negation of And and Or, respectively
• Symbols: | and ↓, respectively
• Nand: p|q ¬(pq)
• Nor: p↓q ¬(pq)
17
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
18
Logical operators:
Conditional 3
Conditional Inverse Converse Contra-
positive
19
p q p q pq pq qp qp
T T F F T T T T
T F F T F T T F
F T T F T F F T
F F T T T T T T
Logical operators:
Conditional 4
• Alternate ways of stating a conditional:
• p implies q
• If p, q
• p is sufficient for q
20
• q if p
• q whenever p
• q is necessary for p
• p only if q
21
Logical operators: Bi-conditional 2
• Let p = “You take this class” and q = “You get a grade”
• Then pq means
“You take this class if p q pq
and only if you get a T T T
grade” T F F
• Alternatively, it means “If F T F
you take this class, then F F T
you get a grade and if you get a grade then you take
(took) this class”
22
Boolean operators summary
not not and or xor nan nor condition bi-
d al condition
al
23
p q p q pq pq pq p|q pq pq pq
T T F F T T F F F T T
T F F T F T T T F F F
F T T F F T T T F T F
F F T T F F F T T T T
• Learn what they mean, don’t just memorize the table!
Precedence of operators
• Just as in algebra, operators have precedence
• 4+3*2 = 4+(3*2), not (4+3)*2
• Precedence order (from highest to lowest): ¬ → ↔
• The first three are the most important
24
• This means that p q ¬r → s ↔ t
yields: (p (q (¬r))) ↔ (s → t)
• Not is always performed before any other operation
Translating English
Sentences
• Problem:
• p = “It is below freezing”
• q = “It is snowing”
25
• It is below freezing and it is snowing pq
• It is below freezing but not snowing p¬q
• It is not below freezing and it is not snowing ¬p¬q
• It is either snowing or below freezing (or both) pq
• p→q
If it is below freezing, it is also snowing
• (pq)(p→¬q)
It is either below freezing or it is snowing,
but it is not snowing if it is below freezing
• That it is below freezing is necessary and p↔q
sufficient for it to be snowing
Translation Example 1
• Heard on the radio:
• A study showed that there was a correlation between the more
children ate dinners with their families and lower rate of
substance abuse by those children
26
• Announcer conclusions:
• If children eat more meals with their family, they will have lower
substance abuse
• If they have a higher substance abuse rate, then they did not eat
more meals with their family
Translation Example 1
• Let p = “Child eats more meals with family”
• Let q = “Child has less substance abuse
• Announcer conclusions:
27
• If children eat more meals with their family, they will
have lower substance abuse
• pq
• If they have a higher substance abuse rate, then they
did not eat more meals with their family
• q p
• Note that p q and q p are logically
equivalent
Translation Example 1
• Let p = “Child eats more meals with family”
• Let q = “Child has less substance abuse”
• Remember that the study showed a correlation,
not a causation
p q result conclusion
T T T T
T F ? F
F T ? T
F F T T
28
Translation Example 2
• “I have neither given nor received help on this
exam”
• Rephrased: “I have not given nor received …”
29
• 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
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”
30
p q pq (pq)
T T F F
T F F F
F T F F
F F T T
31
Bit Operations 2
• Evaluate the following
11000 (01011 11011) 01011
= 11000 (11011) 11011
= 11000 11011
11000
11011
11000
32
&& 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
• If p and q are boolean:
• Both p & q and p && q will be a Boolean operation
• The same holds true for the or operators (| and ||) in both
Java and C/C++
Tautology and Contradiction
• A tautology is a statement that is always true
• p ¬p will always be true (Negation Law)
• A contradiction is a statement that is always false
35
• p ¬p will always be false (Negation Law)
p p ¬p p ¬p
T T F
F T F
Logical Equivalence
• A logical equivalence means that the two sides always have
the same truth values
• Symbol is ≡or
• We’ll use ≡, so as not to confuse it with the bi-conditional
36
Logical Equivalences of And
• pT≡p Identity law
p T pT
T T T
F T F
39
• 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)