About Boolean operators: AND, OR, NOT, XOR

Sometimes we want to describe when a section of a program should be performed. It usually depends on the value of some variable, and often this is a Boolean variable (one which only has the value TRUE or FALSE).

Sometimes the situation is a bit more complex: it depends on more than just one variable. For example if a cinema offers student prices on films on every night except Saturday, the program that decides how much your ticket costs will have to use a condition like "Are they a student AND is it NOT Saturday"

To allow us to cope with these situations we use Boolean or logical expressions.

 

   

Kim is going out on Saturday night. Kim's extensive clubbing wardrobe consists of various tops and bottoms.

 

Kim has decided that fashion and logic can be combined, and we will use this to explain Boolean logic. 

 

Tops:

clothing clothing clothing clothing clothing clothing

tshirt

tshirt tshirt tshirt tshirt tshirt

Bottoms:

bottom bottom bottom bottom bottom
Kim decides to describe Friday night's fashion options as 

TopType="shortsleeve" AND TopColour ="red"

This fairly obviously describes a situation where Kim will wear this:

red tshirt

For Saturday night, Kim's  Boolean expression is:

TopColour= "Black" OR TopColour ="Yellow".

Kim can therefore wear any of these, for this statement to be true:

yellow lsblack tshirtyellow tshirtblack ls

 

If Kim had said TopType="shortsleeve" AND TopType ="longsleeve", things would be a bit difficult: there are NO tops that match this description, as no top has both long and short sleeves. Kim would either be staying home or feeling rather embarrassed at the club.

Kim's best friend Lee has described the clothing for next week's party at the RedEye club. It is:

BottomColour=TopColour AND TopType = "shortsleeve" AND NOT TopColour = "red"

Lee's choices are now a bit more constrained: the AND condition means that all three conditions have to be true. This means that no red tops, only short sleeves, and top and bottom must be the same colour. So Lee's stunning fashion choices are:

   or      or        or        or       
As you can see, Lee's logic ability and fashion sense are not the same.

Kim and Lee met up with Chris at the RedEye Club. Chris describes the clothing rules for next week as:

BottomColour ="purple" XOR TopColour = "purple"

This means that Chris can wear either a purple top or a purple bottom, BUT NOT BOTH. This is not just because it looks atrocious, but because the XOR operator says so. The XOR operator is called an "exclusive OR" which does not allow both conditions to be true. It's a bit like when your parent says you can have a CD or a pair of shoes for your birthday: you can have either but not both.

If the expression had been BottomColour ="purple" XOR TopColour = "purple", either a purple top or a purple bottom or both would meet the requirements.

Kim is by now becoming a bit more choosy about fashion sense. As people put on sunglasses or look the other way when they see some of the clothes combinations, Kim decided to use the following rule:

(NOT TopColour = "green") AND (NOT Topcolour = BottomColour) AND (TopType ="shortsleeve" OR (TopType = longsleeve AND TopColour = "red")).

If we look closely, we see that:

 

Kim, Lee and Chris decide to visit the Paradox Club, run by Terry Boole, a distant relative of mathematician George Boole who developed Boolean logic in the 1850s.

A sign above the door explained the clothing rules for the club:

 

TONIGHT'S DRESS CODE:

(NOT BottomColour =TopColour) AND (TopColour = "purple" OR TopColour ="yellow" OR TopColour = "Red") AND (TopType = "longsleeve" AND (NOT (TopColour ="yellow")) AND (NOT (BottomColour = "purple' OR BottomColor = "black"))

No brains, no entry.

What sort of clothing can they wear to the club?

HINT: you may need to draw pictures to work this out.