TransWikia.com

A beginner example of xor, that is functional (does not switch to procedural)

Computer Science Educators Asked on August 21, 2021

I have been trying to think of an example of xor to connect with what students already know. I did some searches. But everything I found, seems to have unconsciously switched to procedural.

For example:
They say for and “the lights are on and my eyes are open”.
But when they get to xor, they say “Choose this xor choose that”. This is not a boolean predicate that evaluates to true or false. This is a selection.

So the question:
Can you provide an example of use of xor in a boolean statement, that is accessible to students, that are new to boolean?

I am not yet interested in all the beautiful things that can be done with xor. That is easy. At this stage I am trying to connect to what we say in English language.

10 Answers

Parity of a sum

In other words, when is a+b odd and when is it even?

The sum of two integers (a+b) is odd only if exactly one of the two terms (a, b) is odd. If both a and b are odd, then their sum is even. If both a and b are even, then their sum is also even.

Therefore, when trying to find out if a+b is odd, we need to use an xor:

[A+B is odd] = [A is odd] XOR [B is odd]

Compare this to multiplication, where we specifically need both a and b to be odd if a*b is to be odd.

[A*B is odd] = [A is odd] AND [B is odd]

Similarly, when we're looking for products that are even, we use the inverse, which is an or:

[A*B is even] = [A is even] OR [B is even]

At least one of them must be even, but both can be even as well.


Just for completion's sake, if we're looking for even sums, then we have to use the much less commonly used xnor (which is the inverse of xor, where both need to be equal)

[A+B is even] = [A is even] XNOR [B is even]

This sounds complicated, but it usually usually boils down to an equality check:

[A+B is even] = [A is even] == [B is even]

It's hard to come up with natural examples of an xor, for two main reasons:

  • In natural languages, the word "or" generally refers to xor, but not exclusively so. The difference is implied contextually. Compare the following examples:
    • Do you want to buy many toys or one expensive toy?
    • Can Tom or Cindy please come to the front desk please.
    • I like cars that are blue or yellow
    • It's ambiguous whether I like cars that are blue and yellow, or whether I only like cars that are fully yellow or fully blue (this sentence itself is a compounded example of using "or" and whether it means or or xor).
  • In real life, xor is actually quite rare, when you already exclude things like choices or binary states

xor is more commonly encountered in cases where both terms would cancel each other out, as having only one term "active" means that their effect exists and is not canceled out.

Answered by Flater on August 21, 2021

Experimental Approach

This is based on the example given by candied_orange, which I found intriguing because of its simplicity. The example was (slightly edited here):

Say I’ve got two coins. They can each take on two states (heads or tails). Flip em both; xor can tell you if they show different states (not-equal coin sides).

This hypothesis can be empirically verified by a scholar. The coins are physical, everyday objects, which could help to get "in touch" with the topic. Here is what can be observed, by stating the mentioned xor question:

CoinA | CoinB | XOR ("are they different?")
--------------------  
heads | heads | no  
tails | tails | no  
heads | tails | yes  
tails | heads | yes

In Boolean terms this would of course map to heads=0, tails=1, no=0, yes=1 for example.

Replace "they" in the xor-question with the "coin sides" or "states" or "inputs" as it seems fitting for the explanation. Maybe it makes it clearer when talking about "are they both/all different" to underline the "exclusiveness" of xor.

This can also (maybe later on) lead to the awareness that not xor ("not different") is the same as "equality" (== operator) with the paraphrasing question "are they not different (=equal)?". So "all are different" and "all are same" are inverse to each other, logically.

Paraphrasing XOR

The thoughts on the subject of the insufficiency to put the meaning of xor into clear words and the suspected unsuitability of using just "or" sentences, posted by Ben I. with comments by Buffy and ctrl-alt-delor lead to the following idea:

To paraphrase the xor operation, you could use "either or" sentences. As in "Either eggs or pancakes?" But this does still not exactly cover it. To make an "exclusiveness" statement about two objects (=inputs) you need a predicate to build a decision, which was stated in the original post and by ctrl-alt-delor in the comments on another question. An example could be: "What is tastier? Either pancakes or eggs?" which optionally/implicitly should state "Pick one exclusive/single option and discard the other. "Both/All" and "None" would not be valid answers then.

One downside mentioned by Ben I. is, that the predicate of "tastier" does not yield a "true/false" nor a "yes/no" answer, but a "this or that" answer.
Taking the coin example with the "are they different" question yields the desired answer scheme in Boolean terms.

It is quite difficult to find other fitting examples. Imagine having a triangle and a disc cut from paper. The xor questions that come to mind are "Do they both have a sharp tip?", "Which is smaller? The triangle or the disc?", "Are they both made from paper?", "Are they equal?". Each must be carefully evaluated to decide if they generate suited xor statements in Boolean terms.
This can be used as reciprocal exercise to link common language questions to the xor interpretation.

To emphasize the "exclusive" character of xor, it should be made clear, that "if you chose pancakes, you do not get eggs" and vice versa, but this would be the unwanted procedural way of a selection, though.

Answered by Antares on August 21, 2021

Am I leaving a comment or an answer but not both?

Answered by philipxy on August 21, 2021

Here's an example - A 2 way switch

Both 'up' / both 'down' == off

Single 'down' == on

So, in English, one can perhaps say, "The light is on if switch A is down xor switch B is down"

Answered by Suraaj K S on August 21, 2021

I can't tell whether I've missed something in the other answers or comments, but the natural language equivalent of XOR is to ask "is there any difference?"

Used in a sentence which follows the broad format of those in the question, you'd be saying something like "the light is on differs-from the door is open", with the compound 'differs-from' element being the XOR operator.

Even in computer science, the XOR and XNOR operators typically go by other aliases, such as Not-Equal and Equal operators, respectively.

Answered by Steve on August 21, 2021

Let me give a not-answer that may be just about as valuable as an answer. Natural language is ambiguous. Many uses of or in natural language have an xor intent. Your intention, I expect, is to make the lesson clear that the meaning of these two computing operators isn't the same.

It may actually be more dramatic if you use examples that explore the ambiguity in natural language and contrast it with the specific (logic based) meaning in programming that will get the message across more dramatically than simple examples where natural language seems to do the right thing.

So, the answer to "Would you like bacon or sausage?" is "yes - both". And people chuckle (assuming no ethnic or religious restrictions, of course). But the distinction between inclusive and exclusive or is easy to teach noting that we try to avoid such ambiguous constructs in programming languages.

Answered by Buffy on August 21, 2021

The more I think about this, the more that I'm convinced that the or doesn't independently carry the weight of exclusivity in any circumstance outside of choices, and that any exclusivity has to be provided by context of two options that can't coexist.

But the key problem with this for the CS teacher is that in those circumstances, having two true inputs doesn't evaluate to false, it just doesn't make any sense in the first place. This means that the English or will never truly and entirely carry the intuitive behaviors of xor.

I would love to be proven wrong about this, but I don't think I am.

Answered by Ben I. on August 21, 2021

Say I’ve got two coins. They can each take on two states (heads or tails). Flip em both; xor can tell you if they are different (not-equal).

The name has exclusive in it because when xor is yes/1/true, the heads are exclusive. That is, one head excludes the other. This is also true for the tails.

Answered by candied_orange on August 21, 2021

I am going to the local animal shelter. I will adopt a dog xor I will adopt a cat. (I only plan to adopt one pet.)

Answered by paw88789 on August 21, 2021

This was fun! Here are three, and I will update as I think of more:

  1. You may go on the symphony trip if you are in the music history class or if you are not enrolled in any arts class this semester.

  2. You want to find people for your cliff-climbing trip? You're in the wrong place. Ask around in the groups that took the jogging or bike trips up the mountain on Mountain Day, not the group that took the bus trip!

  3. Whether you got here by some connection to the uppity-ups or just by sheer dumb luck, you're my problem now.

Answered by Ben I. on August 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP