1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

42
1 Boolean Logic Boolean Logic CS/APMA 202, Spring 2005 CS/APMA 202, Spring 2005 Rosen, section 1.1 Rosen, section 1.1 Aaron Bloomfield Aaron Bloomfield

description

3 A proposition is a statement that can be either true or false “The sky is blue” “The sky is blue” “I is a English major” “I is a English major” “x == y” “x == y” Not propositions: “Are you Bob?” “Are you Bob?” “x = 7” “x = 7” Boolean propositions

Transcript of 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

Page 1: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

11

Boolean LogicBoolean LogicCS/APMA 202, Spring 2005CS/APMA 202, Spring 2005

Rosen, section 1.1Rosen, section 1.1Aaron BloomfieldAaron Bloomfield

Page 2: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

2

Quick surveyQuick survey How familiar are you with Boolean How familiar are you with Boolean

logic?logic?a)a) A lotA lotb)b) SomeSomec)c) A littleA littled)d) Boolean what?Boolean what?

Page 3: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

33

A proposition is a statement that can be A proposition is a statement that can be either true or falseeither true or false ““The sky is blue”The sky is blue” ““I is a English major”I is a English major” ““x == y”x == y”

Not propositions:Not propositions: ““Are you Bob?”Are you Bob?” ““x = 7”x = 7”

Boolean propositionsBoolean propositions

Page 4: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

44

We use Boolean variables to refer to We use Boolean variables to refer to propositionspropositions Usually are lower case letters starting with p Usually are lower case letters starting with p

(i.e. (i.e. p, q, r, sp, q, r, s, etc.), etc.) A Boolean variable can have one of two A Boolean variable can have one of two

values true (T) or false (F)values true (T) or false (F)

A proposition can be…A proposition can be… A single variable: A single variable: pp An operation of multiple variables: An operation of multiple variables: pp((qqrr))

Boolean variablesBoolean variables

Page 5: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

55

Introduction to Logical OperatorsIntroduction to Logical Operators

About a dozen logical operatorsAbout a dozen logical operators Similar to algebraic operators + * - /Similar to algebraic operators + * - /

In the following examples,In the following examples, pp = “Today is Friday” = “Today is Friday” qq = “Today is my birthday” = “Today is my birthday”

Page 6: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

66

Logical operators: NotLogical operators: Not

A not operation switches (negates) the truth valueA not operation switches (negates) the truth value

Symbol: Symbol: or ~ or ~

In C++ and Java, In C++ and Java, the operand is the operand is !!

pp = “Today is not Friday” = “Today is not Friday”

pp ppTT FFFF TT

Page 7: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

77

Logical operators: AndLogical operators: And

An and operation is true if both operands are trueAn and operation is true if both operands are true

Symbol: Symbol: It’s like the ‘A’ in AndIt’s like the ‘A’ in And

In C++ and Java, In C++ and Java, the operand is the operand is &&&&

ppqq = “Today is Friday and = “Today is Friday and today is my birthday”today is my birthday”

pp qq ppqqTT TT TTTT FF FFFF TT FFFF FF FF

Page 8: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

88

Logical operators: OrLogical operators: Or

An or operation is true if either operands are trueAn or operation is true if either operands are true

Symbol: Symbol:

In C++ and Java, In C++ and Java,

the operand is the operand is ||||

ppqq = “Today is Friday or = “Today is Friday or

today is my birthday (or today is my birthday (or

possibly both)”possibly both)”

pp qq ppqqTT TT TTTT FF TTFF TT TTFF FF FF

Page 9: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

99

Logical operators: Exclusive OrLogical operators: Exclusive Or

An exclusive or operation is true if one of the operands An exclusive or operation is true if one of the operands are true, but false if both are trueare true, but false if both are true

Symbol: Symbol:

Often called XOROften called XOR

ppqq ( (pp qq) ) ¬( ¬(pp qq) )

In Java, the operand is In Java, the operand is ^̂ (but not in C++)(but not in C++)

ppqq = “Today is Friday or today = “Today is Friday or todayis my birthday, but not both”is my birthday, but not both”

pp qq ppqqTT TT FFTT FF TTFF TT TTFF FF FF

Page 10: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

1010

Inclusive Or versus Exclusive Inclusive Or versus Exclusive OrOr

Do these sentences mean inclusive or Do these sentences mean inclusive or exclusive or?exclusive or? Experience with C++ or Java is requiredExperience with C++ or Java is required Lunch includes soup or saladLunch includes soup or salad To enter the country, you need a passport or To enter the country, you need a passport or

a driver’s licensea driver’s license Publish or perishPublish or perish

Page 11: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

1111

Logical operators: Nand and NorLogical operators: Nand and Nor

The negation of And and Or, respectivelyThe negation of And and Or, respectivelySymbols: | and ↓, respectivelySymbols: | and ↓, respectively Nand: Nand: pp||qq ¬( ¬(ppqq)) Nor: Nor: pp↓↓qq ¬( ¬(ppqq))

pp qq ppqq ppqq pp||qq ppqqTT TT TT TT FF FFTT FF FF TT TT FFFF TT FF TT TT FFFF FF FF FF TT TT

Page 12: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

1212

Logical operators: Conditional 1Logical operators: Conditional 1

A conditional means “if A conditional means “if pp then then qq””Symbol: Symbol: ppqq = “If today is = “If today is Friday, then today Friday, then today is my birthday”is my birthday”p→q=¬pp→q=¬pqq

pp qq ppqqTT TT TTTT FF FFFF TT TTFF FF TT

theantecedent

theconsequence

Page 13: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

1313

Logical operators: Conditional 2Logical operators: Conditional 2

Let Let pp = “I am elected” and = “I am elected” and qq = “I will lower taxes” = “I will lower taxes”

I state: I state: p p qq = “If I = “If I am elected, then I am elected, then I will lower taxes”will lower taxes”

Consider all Consider all possibilitiespossibilities

Note that if Note that if pp is false, then is false, then the conditional is true regardless of whether the conditional is true regardless of whether qq is is true or falsetrue or false

pp qq ppqqTT TT TTTT FF FFFF TT TTFF FF TT

Page 14: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

14

Quick surveyQuick survey Does truth table of the conditional Does truth table of the conditional

make sense?make sense?a)a) Yes, alreadyYes, alreadyb)b) MostlyMostlyc)c) Not reallyNot reallyd)d) Huh? I was sleepingHuh? I was sleeping

Page 15: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

1515

Logical operators: Conditional 3Logical operators: Conditional 3

ConditionalConditional InverseInverse ConverseConverse Contra-Contra-positivepositive

pp qq pp qq ppqq ppqq qqpp qqppTT TT FF FF TT TT TT TTTT FF FF TT FF TT TT FFFF TT TT FF TT FF FF TTFF FF TT TT TT TT TT TT

Page 16: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

1616

Logical operators: Conditional 4Logical operators: Conditional 4

Alternate ways of stating a conditional:Alternate ways of stating a conditional: pp implies implies qq If If pp, , qq pp only if only if qq pp is sufficient for is sufficient for qq qq if if pp qq whenever whenever pp qq is necessary for is necessary for pp pp only if only if qqSee Rosen, p. 6, for a full listSee Rosen, p. 6, for a full list

I don’t like this one

Page 17: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

1818

Logical operators: Bi-conditional 1Logical operators: Bi-conditional 1

A bi-conditional means “A bi-conditional means “pp if and only if if and only if qq””Symbol: Symbol: Alternatively, it means Alternatively, it means “(if “(if pp then then qq) and ) and (if (if qq then then pp)”)”Note that a bi-conditional Note that a bi-conditional has the opposite truth values has the opposite truth values of the exclusive orof the exclusive or

pp qq ppqqTT TT TTTT FF FFFF TT FFFF FF TT

Page 18: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

1919

Logical operators: Bi-conditional 2Logical operators: Bi-conditional 2

Let Let pp = “You take this class” and = “You take this class” and qq = “You get a = “You get a grade”grade”Then Then ppqq means means “You take this class if “You take this class if and only if you get a and only if you get a grade”grade”Alternatively, it means “If Alternatively, it means “If you take this class, then you take this class, then you get a grade and if you get a grade then you you get a grade and if you get a grade then you take (took) this class”take (took) this class”

pp qq ppqqTT TT TTTT FF FFFF TT FFFF FF TT

Page 19: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

2020

Boolean operators summaryBoolean operators summary

Learn what they mean, don’t just Learn what they mean, don’t just memorize the table!memorize the table!

notnot notnot andand oror xorxor nandnand nornor conditionalconditional bi-bi-conditionalconditional

pp qq pp qq ppqq ppqq ppqq pp||qq ppqq ppqq ppqqTT TT FF FF TT TT FF FF FF TT TT

TT FF FF TT FF TT TT FF TT FF FF

FF TT TT FF FF TT TT FF TT TT FF

FF FF TT TT FF FF FF TT TT TT TT

Page 20: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

21

Quick surveyQuick survey I understand all the Boolean I understand all the Boolean

operatorsoperatorsa)a) Absolutely!Absolutely!b)b) With a little review after class, I’ll With a little review after class, I’ll

have it downhave it downc)c) Can we go over it again?Can we go over it again?d)d) 1717

Page 21: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

2222

Precedence of operatorsPrecedence of operators

Just as in algebra, operators have Just as in algebra, operators have precedenceprecedence 4+3*2 = 4+(3*2), not (4+3)*24+3*2 = 4+(3*2), not (4+3)*2Precedence order (from highest to lowest): Precedence order (from highest to lowest): ¬ ¬ → ↔ → ↔ The first three are the most importantThe first three are the most importantThis means that This means that pp qq ¬ ¬rr → → ss ↔ ↔ tt yields: (yields: (pp ( (qq (¬ (¬rr))) ↔ ())) ↔ (ss → → tt))Not is Not is alwaysalways performed before any other performed before any other operationoperation

Page 22: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

2323

Translating English SentencesTranslating English SentencesQuestion 7 from Rosen, p. 16Question 7 from Rosen, p. 16

pp = “It is below freezing” = “It is below freezing” qq = “It is snowing” = “It is snowing”

It is below freezing and it is snowingIt is below freezing and it is snowingIt is below freezing but not snowingIt is below freezing but not snowingIt is not below freezing and it is not snowingIt is not below freezing and it is not snowingIt is either snowing or below freezing (or both)It is either snowing or below freezing (or both)If it is below freezing, it is also snowingIf it is below freezing, it is also snowingIt is either below freezing or it is snowing, It is either below freezing or it is snowing, but it is not snowing if it is below freezingbut it is not snowing if it is below freezingThat it is below freezing is necessary and That it is below freezing is necessary and sufficient for it to be snowingsufficient for it to be snowing

ppqqpp¬¬qq¬¬pp¬¬qqppqqpp→→qq

((ppqq))((pp→¬→¬qq))

pp↔↔qq

Page 23: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

2424

Translation Example 2Translation Example 2

Heard on the radio:Heard on the radio: A study showed that there was a correlation A study showed that there was a correlation

between the more children ate dinners with between the more children ate dinners with their families and lower rate of substance their families and lower rate of substance abuse by those childrenabuse by those children

Announcer conclusions:Announcer conclusions:If children eat more meals with their family, they If children eat more meals with their family, they will have lower substance abusewill have lower substance abuseIf they have a higher substance abuse rate, then If they have a higher substance abuse rate, then they did not eat more meals with their familythey did not eat more meals with their family

Page 24: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

2525

Translation Example 2Translation Example 2

Let Let pp = “Child eats more meals with family” = “Child eats more meals with family”Let Let qq = “Child has less substance abuse = “Child has less substance abuseAnnouncer conclusions:Announcer conclusions: If children eat more meals with their family, they will If children eat more meals with their family, they will

have lower substance abusehave lower substance abusepp qq

If they have a higher substance abuse rate, then they If they have a higher substance abuse rate, then they did not eat more meals with their familydid not eat more meals with their family

qq pp

Note that Note that pp qq and and qq pp are logically are logically equivalentequivalent

Page 25: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

2626

Translation Example 2Translation Example 2

Let Let pp = “Child eats more meals with family” = “Child eats more meals with family”Let Let qq = “Child has less substance abuse” = “Child has less substance abuse”

pp qq resultresult conclusionconclusionTT TT TT TTTT FF ?? FFFF TT ?? TTFF FF TT TT

Page 26: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

2727

Translation Example 3Translation Example 3

““I have neither given nor received help on this I have neither given nor received help on this exam”exam” Rephrased: “I have not given nor received …”Rephrased: “I have not given nor received …” Let Let pp = “I have given help on this exam” = “I have given help on this exam” Let Let qq = “I have received help on this exam” = “I have received help on this exam”

Translation is: Translation is: ppqqpp qq pp ppqqTT TT FF FFTT FF FF TTFF TT TT FFFF FF TT FF

Page 27: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

2828

Translation Example 3Translation Example 3What they mean is “I have not given and I have What they mean is “I have not given and I have not received help on this exam”not received help on this exam” Or “I have not (given nor received) help on this exam”Or “I have not (given nor received) help on this exam”

The problem: The problem: has a higher precedence than has a higher precedence than in Boolean logic, but not always in Englishin Boolean logic, but not always in EnglishAlso, “neither” is vagueAlso, “neither” is vague

pp qq ppqq ((ppqq))TT TT FF FFTT FF FF FFFF TT FF FFFF FF TT TT

Page 28: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

2929

A bit of humor:A bit of humor:Yale vs. HarvardYale vs. Harvard

Web references: http://www.harvardsucks.org/, Web references: http://www.harvardsucks.org/, http://www.yaledailynews.com/article.asp?AID=27506http://www.yaledailynews.com/article.asp?AID=27506

Page 29: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

3030

Just like translating English sentencesJust like translating English sentencesIn order for a system specification to be valid, there must be at least In order for a system specification to be valid, there must be at least ONE truth assignment that allows the propositions to be trueONE truth assignment that allows the propositions to be trueQuestion 43 from Rosen, p. 10Question 43 from Rosen, p. 10

pp = “the message is scanned for virii” = “the message is scanned for virii” qq = “the message was sent from and unknown system” = “the message was sent from and unknown system”

The message is scanned for virii whenever theThe message is scanned for virii whenever themessage was sent from an unknown systemmessage was sent from an unknown systemThe message was sent from an unknownThe message was sent from an unknownsystem but it was not scanned for viriisystem but it was not scanned for viriiIt is necessary to scan the message for viriiIt is necessary to scan the message for viriiwhenever it was sent from an unknown systemwhenever it was sent from an unknown systemWhen a message is not sent from an unknownWhen a message is not sent from an unknownsystem, it is not scanned for viriisystem, it is not scanned for virii

System Specifications 1System Specifications 1

qq→→pp

qq¬¬pp

qq→→pp

¬¬qq→¬→¬pp

equal

Page 30: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

3131

System Specifications 2System Specifications 2

Is this a valid system?Is this a valid system? pp = “the message is scanned for virii” = “the message is scanned for virii” qq = “the message was sent from and unknown system” = “the message was sent from and unknown system”

Requirements: (Requirements: (qq→→pp) ) ( (qq¬¬pp) ) (¬ (¬qq→¬→¬pp))

pp qq qqpp qqpp qqpp

TT TT TT FF TTTT FF TT FF FFFF TT FF TT TTFF FF TT FF TT

Page 31: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

32

Quick surveyQuick survey Is this a valid system?Is this a valid system?

a)a) YesYesb)b) NoNoc)c) Need more informationNeed more informationd)d) I have no ideaI have no ideapp qq qqpp qqpp qqppTT TT TT FF TTTT FF TT FF FFFF TT FF TT TTFF FF TT FF TT

Page 32: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

3333

System Specifications 2System Specifications 2

The system as specified is not validThe system as specified is not valid There is no assignment of truth values that There is no assignment of truth values that

makes all the system specs truemakes all the system specs true

pp qq qqpp qqpp qqpp

TT TT TT FF TTTT FF TT FF FFFF TT FF TT TTFF FF TT FF TT

Page 33: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

3434

System Specifications 2System Specifications 2Let’s remove one of the specifciations:Let’s remove one of the specifciations:

qq¬¬pp = The message was sent from an unknown system but it = The message was sent from an unknown system but it was not scanned for viriiwas not scanned for virii

Is this a valid system?Is this a valid system? Requirements: (Requirements: (qq→→pp) ) (¬ (¬qq→¬→¬pp))

pp qq qqpp qqpp qqpp

TT TT TT FF TTTT FF TT FF FFFF TT FF TT TTFF FF TT FF TT

Page 34: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

3535

Boolean SearchesBoolean Searches

(101 OR 202) AND bloomfield (101 OR 202) AND bloomfield AND AND “computer science” “computer science”

Note that Google requires you to capitalize Note that Google requires you to capitalize Boolean operatorsBoolean operators

Google defaults to AND; many others do Google defaults to AND; many others do notnot

Page 35: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

3636

Bit Operations 1Bit Operations 1

Boolean values can be represented as 1 (true) Boolean values can be represented as 1 (true) and 0 (false)and 0 (false)A bit string is a series of Boolean valuesA bit string is a series of Boolean values 10110100 is eight Boolean values in one string10110100 is eight Boolean values in one string

We can then do operations on these Boolean We can then do operations on these Boolean stringsstrings Each column is its ownEach column is its own

Boolean operationBoolean operation0101101001011010

10110100101101001110111011101110

Page 36: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

3737

Bit Operations 2Bit Operations 2

Evaluate the followingEvaluate the following 11000 11000 (01011 (01011 11011) 11011)= 11000 = 11000 (11011) (11011)= 11000= 11000

010110101111011110111101111011

110001100011011110111100011000

Page 37: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

38

Quick surveyQuick survey What programming languages do What programming languages do

you know?you know?a)a) Java onlyJava onlyb)b) C / C++ onlyC / C++ onlyc)c) Java and C/C++Java and C/C++d)d) not (Java or C/C++)not (Java or C/C++)

Page 38: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

3939

&& vs. & in C/C++&& vs. & in C/C++

int p = 11;int p = 11;int q = 20;int q = 20;if ( p && q ) {if ( p && q ) {}}if ( p & q ) {if ( p & q ) {}}

In C/C++, any value other than 0 is trueIn C/C++, any value other than 0 is true

Notice the double ampersand – this is a Boolean operationNotice the double ampersand – this is a Boolean operation

The binary for the integer 11 is 01011The binary for the integer 11 is 01011

The binary for the integer 20 is 10100The binary for the integer 20 is 10100

As p and q are both true, this is trueAs p and q are both true, this is trueNotice the single ampersand – this is a bitwise operationNotice the single ampersand – this is a bitwise operation

010110101110100101000000000000

This evaluates to This evaluates to zero (false)!zero (false)!

Consider the Consider the following:following:

Bitwise Boolean Bitwise Boolean And operation:And operation:

Page 39: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

4040

&& vs. & in C/C++&& vs. & in C/C++

Note that Java does not have this “feature”Note that Java does not have this “feature” If If pp and and qq are int: are int:

p & q p & q is bitwiseis bitwisep && q p && q will not compilewill not compile

If If pp and and qq are boolean: are boolean:Both Both p & qp & q and and p && qp && q will be a Boolean will be a Boolean operationoperation

The same holds true for the or operators The same holds true for the or operators ((|| and and ||||) in both Java and C/C++) in both Java and C/C++

Page 40: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

41

Quick surveyQuick survey I felt I understood the material in I felt I understood the material in

this slide set…this slide set…a)a) Very wellVery wellb)b) Pretty muchPretty muchc)c) Not reallyNot reallyd)d) Not at allNot at all

Page 41: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

42

Quick surveyQuick survey The pace of the lecture for this The pace of the lecture for this

slide set was…slide set was…a)a) Too slowToo slowb)b) A little slowA little slowc)c) About rightAbout rightd)d) FastFast

Page 42: 1 Boolean Logic CS/APMA 202, Spring 2005 Rosen, section 1.1 Aaron Bloomfield.

4343

Today’s demotivatorsToday’s demotivators