0% found this document useful (0 votes)
9 views25 pages

Boolean Logic

Uploaded by

itzbelyse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views25 pages

Boolean Logic

Uploaded by

itzbelyse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

INTRODUCING

COMPUTER SYSTEMS
COMP-111 BOOLEAN LOGIC
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 am 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(qr)
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”
LOGICAL OPERATORS: NOT
6

• A not operation switches (negates) the truth


p p
value
T F
• Symbol:  or ~
F T
• In C++ and Java,
the operand is !
• p = “Today is not Friday”
LOGICAL OPERATORS: AND
7

• An and operation is true if both operands


p q pq
are true
T T T
• Symbol: 
T F F
• It’s like the ‘A’ in And
• In C++ and Java, F T F
the operand is && F F F
• pq = “Today is Friday and
today is my birthday”
LOGICAL OPERATORS: OR
8

• An or operation is true if either operands p q pq


are true T T T
• Symbol: 
T F T
• In C++ and Java,
the operand is || F T T
• pq = “Today is Friday or F F F
today is my birthday (or
possibly both)”
LOGICAL OPERATORS: EXCLUSIVE OR
9

• An exclusive or operation is true if one of the p q pq


operands are true, but false if both are true
T T F
• Symbol: 
• Often called XOR T F T
• pq  (p  q)  ¬(p  q)
F T T
• In Java, the operand is ^
(but not in C++) F F F
• pq = “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
LOGICAL OPERATORS: NAND AND NOR
11

• The negation of And and Or, respectively


• Symbols: | and ↓, respectively
• Nand: p|q  ¬(pq)
• Nor: p↓q  ¬(pq)
p q pq pq p|q pq
T T T T F F
T F F T T F
F T F T T F
F F F F T T
LOGICAL OPERATORS: CONDITIONAL 1
12

• A conditional means “if p then q”


• Symbol:  p q pq ¬pq
• pq = “If today is T T T T
Friday, then today T F F F
is my birthday” F T T T
• p→q  ¬pq F F T T

the the
antecedent consequence
LOGICAL OPERATORS: CONDITIONAL 2
13

• Let p = “I am elected” and q = “I will lower taxes”


• I state: p  q = “If I p q pq
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

Conditional Inverse Converse Contra-


positive
p q p q pq pq qp qp
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
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
LOGICAL OPERATORS: BI-CONDITIONAL 1
16

• A bi-conditional means “p if and only if q”


• Symbol: 
p q pq
• Alternatively, it means
T T T
“(if p then q) and
T F F
(if q then p)”
F T F
• pq  pq  qp
F F T
• Note that a bi-conditional
has the opposite truth values
of the exclusive or
LOGICAL OPERATORS: BI-CONDITIONAL 2
17

• Let p = “You take this class” and q = “You get a grade”


• Then pq means p q pq
“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

not not and or xor nand nor conditional bi-


conditional
p q p q 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 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!


19 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
• This means that p  q  ¬r → s ↔ t
yields: (p  (q  (¬r))) ↔ (s → t)
• Not is always performed before any other operation
20 TRANSLATING ENGLISH SENTENCES
• Problem:
• p = “It is below freezing”
• q = “It is snowing”

• 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 ¬p¬q
• It is either snowing or below freezing (or both) pq
• If it is below freezing, it is also snowing p→q
• It is either below freezing or it is snowing, (pq)(p→¬q)
but it is not snowing if it is below freezing
• That it is below freezing is necessary and sufficient p↔q
for it to be snowing
21 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
• 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
22 TRANSLATION EXAMPLE 1

• 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


TRANSLATION EXAMPLE 1
23

• 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
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: pq p q p pq


T T F F
T F F T
F T T F
F F T F
25 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 pq (pq)
T T F F
T F F F
F T F F
F F T T
• The problem:  has a higher precedence than  in
Boolean logic, but not always in English
• Also, “neither” is vague

You might also like