Boolean logic (§ 1.1)

advertisement
Boolean Logic
CS/APMA 202, Spring 2005
Rosen, section 1.1
Aaron Bloomfield
1
Quick survey

How familiar are you with Boolean logic?
a)
b)
c)
d)
A lot
Some
A little
Boolean what?
2
Boolean propositions
A proposition is a statement that can be
either true or false



“The sky is blue”
“I is a English major”
“x == y”
Not propositions:


“Are you Bob?”
“x = 7”
3
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(qr)
4
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”
5
Logical operators: Not
A not operation switches (negates) the truth value
Symbol:  or ~
In C++ and Java,
the operand is !
p
T
F
p
F
T
p = “Today is not Friday”
6
Logical operators: And
An and operation is true if both operands are true
Symbol: 

It’s like the ‘A’ in And
In C++ and Java,
the operand is &&
pq = “Today is Friday and
today is my birthday”
p
T
T
F
F
q
T
F
T
F
pq
T
F
F
F
7
Logical operators: Or
An or operation is true if either operands are true
Symbol: 
In C++ and Java,
the operand is ||
pq = “Today is Friday or
today is my birthday (or
p
T
T
F
F
q
T
F
T
F
pq
T
T
T
F
possibly both)”
8
Logical operators: Exclusive Or
An exclusive or operation is true if one of the operands
are true, but false if both are true
Symbol: 
Often called XOR
pq  (p  q)  ¬(p  q)
In Java, the operand is ^
(but not in C++)
pq = “Today is Friday or today
is my birthday, but not both”
p
T
T
F
F
q
T
F
T
F
pq
F
T
T
F
9
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
10
Logical operators: Nand and Nor
The negation of And and Or, respectively
Symbols: | and ↓, respectively


Nand: p|q  ¬(pq)
Nor: p↓q  ¬(pq)
p
T
T
q
T
F
F
F
T
F
pq pq
T
T
F
T
F
F
T
F
p|q
F
T
pq
F
F
T
T
F
T
11
Logical operators: Conditional 1
A conditional means “if p then q”
Symbol: 
pq = “If today is
Friday, then today
is my birthday”
p→q=¬pq
the
antecedent
p
T
T
F
F
q
T
F
T
F
pq
T
F
T
T
the
consequence
12
Logical operators: Conditional 2
Let p = “I am elected” and q = “I will lower taxes”
I state: p  q = “If I
am elected, then I
will lower taxes”
Consider all
possibilities
p
T
T
F
F
q
T
F
T
F
pq
T
F
T
T
Note that if p is false, then
the conditional is true regardless of whether q is
true or false
13
Quick survey

Does truth table of the conditional make
sense?
a)
b)
c)
d)
Yes, already
Mostly
Not really
Huh? I was sleeping
14
Logical operators: Conditional 3
p
T
q
T
T
F
F
F
T
F
p q
F
F
F
T
T
T
F
T
Conditional
Inverse
Converse
Contrapositive
pq
T
pq
T
qp
T
qp
T
F
T
T
T
F
T
T
F
T
F
T
T
15
Logical operators: Conditional 4
Alternate ways of stating a conditional:








p implies q
If p, q
p only if 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
See Rosen, p. 6, for a full list
16
Logical operators: Bi-conditional 1
A bi-conditional means “p if and only if q”
Symbol: 
p
q
Alternatively, it means
T
T
“(if p then q) and
T
F
(if q then p)”
F
T
Note that a bi-conditional
has the opposite truth values
F
F
of the exclusive or
pq
T
F
F
T
18
Logical operators: Bi-conditional 2
Let p = “You take this class” and q = “You get a
grade”
p
q pq
Then pq means
T
T
T
“You take this class if
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”
19
Boolean operators summary
not
not
and
or
pq pq
xor
nand
nor
conditional
biconditional
pq
p|q
pq
pq
pq
p
q
p
q
T
T
F
F
T
T
F
F
F
T
T
T
F
F
T
F
T
T
F
T
F
F
F
T
T
F
F
T
T
F
T
T
F
F
F
T
T
F
F
F
T
T
T
T
Learn what they
memorize the table!
mean,
don’t
just
20
Quick survey

I understand all the Boolean operators
a)
b)
c)
d)
Absolutely!
With a little review after class, I’ll have it down
Can we go over it again?
17
21
Precedence of operators
Just as in
precedence

algebra,
operators
have
4+3*2 = 4+(3*2), not (4+3)*2
Precedence order (from highest to lowest):
¬→↔

The first three are the most important
This means that p  q  ¬r → s ↔ t
yields: (p  (q  (¬r))) ↔ (s → t)
Not is always performed before any other
operation
22
Translating English Sentences
Question 7 from Rosen, p. 16


p = “It is below freezing”
q = “It is snowing”
pq
It is below freezing and it is snowing
p¬q
It is below freezing but not snowing
¬p¬q
It is not below freezing and it is not snowing
It is either snowing or below freezing (or both) pq
p→q
If it is below freezing, it is also snowing
(pq)(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
23
Translation Example 2
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
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
24
Translation Example 2
Let p = “Child eats more meals with family”
Let q = “Child has less substance abuse
Announcer conclusions:

If children eat more meals with their family, they will
have lower substance abuse
pq

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
25
Translation Example 2
Let p = “Child eats more meals with family”
Let q = “Child has less substance abuse”
p
T
T
F
F
q
T
F
T
F
result
T
?
?
T
conclusion
T
F
T
T
26
Translation Example 3
“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: pq
p
T
q
T
p
F
pq
F
T
F
F
F
T
F
F
T
T
T
F
F
27
Translation Example 3
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
T
q
T
pq
F
(pq)
F
T
F
F
F
T
F
F
F
T
F
F
T
The problem:  has a higher precedence than 
in Boolean logic, but not always in English
Also, “neither” is vague
28
A bit of humor:
Yale vs. Harvard

Web references: http://www.harvardsucks.org/,
http://www.yaledailynews.com/article.asp?AID=27506
29
System Specifications 1
Just like translating English sentences
In order for a system specification to be valid, there must be at least
ONE truth assignment that allows the propositions to be true
Question 43 from Rosen, p. 10


p = “the message is scanned for virii”
q = “the message was sent from and unknown system”
The message is scanned for virii whenever the
message was sent from an unknown system
The message was sent from an unknown
system but it was not scanned for virii
It is necessary to scan the message for virii
whenever it was sent from an unknown system
When a message is not sent from an unknown
system, it is not scanned for virii
q→p
q¬p
equal
q→p
¬q→¬p
30
System Specifications 2
Is this a valid system?


p = “the message is scanned for virii”
q = “the message was sent from and unknown system”
Requirements: (q→p)  (q¬p)  (¬q→¬p)
p
q
qp
qp
qp
T
T
F
T
F
T
T
T
F
F
F
T
T
F
T
F
F
T
F
T
31
Quick survey

Is this a valid system?
Yes
No
Need more information
I have no idea
a)
b)
c)
d)
p
q
qp
qp
qp
T
T
F
F
T
F
T
F
T
T
F
T
F
F
T
F
T
F
T
T
32
System Specifications 2
The system as specified is not valid

There is no assignment of truth values that
makes all the system specs true
p
q
qp
qp
qp
T
T
F
T
F
T
T
T
F
F
F
T
T
F
T
F
F
T
F
T
33
System Specifications 2
Let’s remove one of the specifciations:

q¬p = The message was sent from an unknown system but it
was not scanned for virii
Is this a valid system?

Requirements: (q→p)  (¬q→¬p)
p
q
qp
qp
qp
T
T
T
F
T
T
F
F
T
F
F
F
T
F
F
T
T
F
T
T
34
Boolean Searches
(101 OR 202) AND bloomfield AND
“computer science”
Note that Google requires you to capitalize
Boolean operators
Google defaults to AND; many others do
not
35
Bit Operations 1
Boolean values can be represented as 1 (true)
and 0 (false)
A bit string is a series of Boolean values

10110100 is eight Boolean values in one string
We can then do operations on these Boolean
strings
 Each column is its own
01011010
Boolean operation
10110100
11101110
36
Bit Operations 2
Evaluate the following
11000  (01011  11011)
= 11000  (11011)
= 11000
01011
11011
11011
11000
11011
11000
37
Quick survey

What programming languages do you know?
a)
b)
c)
d)
Java only
C / C++ only
Java and C/C++
not (Java or C/C++)
38
&& vs. & in C/C++
Consider the
following:
In C/C++, any value other than 0 is true
int p = 11;
int q = 20;
if ( p && q ) {
}
if ( p & q ) {
}
The binary for the integer 20 is 10100
Notice the double ampersand – this is a
Boolean operation
As p and q are both true, this is true
Notice the single ampersand – this is a
bitwise operation
The binary for the integer 11 is 01011
01011
Bitwise Boolean
10100
And operation:
00000
This evaluates to
zero (false)!
39
&& vs. & in C/C++
Note that Java does not have this “feature”

If p and q are int:
p & q is bitwise
p && q will not compile

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++
40
Quick survey

I felt I understood the material in this slide
set…
a)
b)
c)
d)
Very well
Pretty much
Not really
Not at all
41
Quick survey

The pace of the lecture for this slide set was…
a)
b)
c)
d)
Too slow
A little slow
About right
Fast
42
Today’s demotivators
43
Download