7
$\begingroup$

How can I illustrate the difference between $\lor$ and $\oplus$ in a way that isn't just boring? Are there fun ways to illustrate and provide practice for this small, yet key, distinction?

$\endgroup$
  • 2
    $\begingroup$ Do you mean in logic computations, specifically or as an operation in programming? Do you mean also "short circuit" evaluation? $\endgroup$ – Buffy Jul 31 '17 at 16:19
  • 1
    $\begingroup$ Well, exclusive or kind of matches people's intuition of what "or" means, inclusive or is slightly more counterintuitive. $\endgroup$ – EJoshuaS - Reinstate Monica Jul 31 '17 at 16:53
  • 1
    $\begingroup$ For the "What's the point" kind of questions, I would point out how in English as it is commonly practiced, context can change the meaning of the conjunctions. "AND" can sometimes mean "OR". For example: "You have two choices: You can go to the party, and you can stay home." Everyone hearing it would understand you are listing exclusive options. In this case, "AND" means "XOR". $\endgroup$ – Charlie Kilian Jul 31 '17 at 21:27
  • 1
    $\begingroup$ OR and AND are ambiguous in English: "You can hang your sign here if it's blue or if it's purple." "You can hang your sign here if it's blue and if it's purple." They mean the same thing. The point of rigorously defining the binary (and logical) operators is so they mean one thing and only one thing, to make sure you're telling the computer exactly what you want it to do. $\endgroup$ – Charlie Kilian Jul 31 '17 at 21:30
  • 1
    $\begingroup$ A 3-way (4-way, multiway) light switch can model a xor operation. When both switches are down, the light is out, when one switch is up, the light is on, and when both switches are up, the light is back off. In multiway configurations, the system behaves as the generic definition of the xor function: if the number of switches that are up is odd, then the light is on, else, the light is off. This is all assuming that no switch has been installed upside down, of course. $\endgroup$ – sleblanc Aug 1 '17 at 3:30
7
$\begingroup$

Play the “if you have brown hair stand up” game.

For the introductory lesson. We use this to introduce selection (if statements), and a little bit of boolean logic.

“Stand up if you have got brown hair.”
“Sit down.” look around “Stand up if you have blue eyes.”

With out asking them to sit down “Stand up if you …”
Address why some people are still standing, are they correct [yes], or are the seated people correct [no]. Why? [because I did not ask them to sit].

“stand up if you have brown hair or you are …” (this is where we start to cover Boolean logic, I have only done and, or, not).

In another lesson you could add xor, nand, nor.

Xor examples

“if you have blond hair or you have blue eyes stand up”
“if you have blond hair xor you have blue eyes stand up”

$\endgroup$
  • 6
    $\begingroup$ My favorite example of Null: "Everyone who is not here, raise your hand." $\endgroup$ – user737 Jul 31 '17 at 19:43
5
$\begingroup$

You might go about explaining it by using the words "either" and "or".

"Either" means that any option out of two available would be enough:

perfect indifference of the two (or more) things.

But also

to emphasize the mutual exclusiveness: either of the two, but not both.

(taken from Oxford English Dictionary).

This means that xor is true only when either one is, but not when neither are true or neither are false. The key point to stress is "either one".

As for practice, you can ask the students to come up with 3-4 properties (boy\girl, reads many books\doesn't read many books, etc.) that some students have and some don't.

For the first property, ask every student who has that property (can't think of a better word :/) to write in their notebooks the numer 1 (they might ask "where should I write it?", it doesn't matter that much as long as there's room for a few more numbers). Students who don't have that property should write 0.

Then go through the other properties (or conditions) and for each one, ask every student who has the current property to do the following:

If they have written the number 1 for the previous property, to write the number 0 (maybe next to the 1)
Every student who didn't write 1, should do so.

Every student who doesn't have the current propery, should copy what they wrote for the last property.

After reaching the fourth property, ask students to check if the last number is 1. If it is, ask them to reflect whether they have either one of the properties, but not an even number. Explain that this is what xor does: only if an odd number of conditions is fullfilled, is the result true.

Now, after they understand this point, go through the 4 properties again but this time ask the students to write 1 as soon as you get to a property they have, and 0 if they don't have the current property.

Every student that has a 1, is a student that an OR operator would result in true, had they been passed as a parameter (if they are high-school students, they might find being called "parameters" intriguing and this might make more students listen during the lesson).

By comparing the results for the first practice with the one from the second practice, the difference between OR and XOR becomes quite clear.

Another way to make it unquestionably clear is to show the truth table of each operator.

(you iterate the properties)

$\endgroup$
4
$\begingroup$

I enjoy using cartoons in my teaching, such as this one:

Boolean Hair Logic

You can also teach students the XOR trick for swapping two values without using intermediate storage:

X := X XOR Y
Y := X XOR Y
X := X XOR Y

See correctness proof.

$\endgroup$
3
$\begingroup$

Think pizza shop. You order a one-topping pie. Would you like pepperoni or sausage? In common parlance, that's an exclusive or.

$\endgroup$
  • $\begingroup$ I'm pretty sure that's inclusive or, because "both" would be a fine answer. "Would you like a white pie or pie with sauce" would be exclusive. $\endgroup$ – Ben I. Aug 1 '17 at 13:31
  • $\begingroup$ You will notice I said "one-topping pie" $\endgroup$ – ncmathsadist Aug 1 '17 at 22:00
  • $\begingroup$ You give me far too much credit, sir. ;-D $\endgroup$ – Ben I. Aug 1 '17 at 22:02
  • $\begingroup$ Do you happen to know of a situation analogous to (or English equivalent of) $A \oplus B \oplus C$? $\endgroup$ – Ben I. Aug 1 '17 at 22:48
  • $\begingroup$ This means an odd number of A, B and C ar true. $\endgroup$ – ncmathsadist Aug 1 '17 at 22:50
2
$\begingroup$

I wouldn't bother trying to tie this in to English language. Exclusive or is a strange connective. For example:

$a \oplus (b \oplus c)$

means "An odd number of ($a$, $b$, $c$) is true" (you can see this by working out the truth table). In general if you stack up disjuncts like this, exclusive or means "an odd number of these disjuncts is true"; it is the binary odd counting function.

No connective in natural language behaves in this way, and definitely not English 'or'. For this reason, you should not use the English language to clarify exclusive or.

The 'or' in "You may have ice cream or cake (but not both)" is not even a disjunction, because you can infer from it that you may have ice cream. From a real disjunction like "He is in Paris or London", you can't infer that he is in Paris. Unfortunately, most elementary logic textbooks get this wrong; this highlights the confusion you get when you try to make formal logic clearer by comparing it to natural language. Math is perfectly clear; natural language is a complete mess : )

I think the simplest clarification of the difference between these two connectives is by comparing it to the difference between "At least one of these two things is true", and "Exactly one of these things is true".

$\endgroup$
  • $\begingroup$ Welcome to Computer Science Educators! A very interesting point. But I don't quite see how this answers the question. Could you please edit your answer to add an example for illustrating the difference between OR and XOR? $\endgroup$ – ItamarG3 Jul 31 '17 at 21:50
  • $\begingroup$ It is interesting that I have told my fellow programmers to not do if condition == true, because we don't say “would you like fries with that is true”, but here I see this in natural language. As I am from only one part of the English speaking world, and the way we speak can change in less than 100 miles. I would like to ask. Do people add “Is true” to the end of questions, where you are from? — thanks $\endgroup$ – ctrl-alt-delor Jul 31 '17 at 21:51
  • $\begingroup$ This really is an interesting point, but while I agree that I can't think of any English construction comparable to $A\oplus (B \oplus C)$, why is $A \oplus B$ not the same has he is in Paris or London? $\endgroup$ – Ben I. Aug 1 '17 at 0:07
  • $\begingroup$ @Ben Just because we think it's the same "or" as "he is in Paris or London or Istanbul". Clearly it can't be mathematical ex-or. $\endgroup$ – mander Aug 3 '17 at 22:05
  • $\begingroup$ @mander I agree that "He is in Paris or London or Istanbul" is not equivalent to $inParis \oplus inLondon \oplus inIstanbul$, because that is a matter of 3 $\oplus$s. But "He is in Paris or London" should be an identical statement to $inParis \oplus inLondon$. $\endgroup$ – Ben I. Aug 3 '17 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.