6
$\begingroup$

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.

$\endgroup$
15
  • 2
    $\begingroup$ I'm not sure I understand. In natural language most usages of "or" have an "xor" implication. "Give me liberty xor give me death". Is that what you mean or do I miss something? $\endgroup$
    – Buffy
    Commented Apr 23, 2020 at 10:28
  • 1
    $\begingroup$ Actually, though, when said by a natural language speaker it is taken as a statement of fact. I.e. True. But to warp natural language why not "the lights are on xor my eyes are open". Though a speaker would probably use "or" when meaning "xor". $\endgroup$
    – Buffy
    Commented Apr 23, 2020 at 11:07
  • 1
    $\begingroup$ multiway switching? en.wikipedia.org/wiki/Multiway_switching $\endgroup$ Commented Apr 23, 2020 at 13:29
  • 2
    $\begingroup$ Good luck! IMO, the trouble you're having is that XOR is an unfamiliar concept to most people. It's easy to construct an English language assertion that uses it. E.g.; "Either my house has five rooms or my house is painted red, but both of those things are not true." That's declarative, and it's testable, and it's completely unlike anything that anybody ever says outside of the context of some logic puzzle. $\endgroup$ Commented Apr 23, 2020 at 13:34
  • 1
    $\begingroup$ Actually, there's a problem with, say, unique choice questions. You have to tick A, B, or C, but only one of them. That's not A xor B xor C. $\endgroup$ Commented Apr 25, 2020 at 11:00

10 Answers 10

4
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ Yes! "Does this differ from that?" is truly xor. I don't think that or can hold that relationship in English. $\endgroup$
    – Ben I.
    Commented Apr 25, 2020 at 17:00
  • $\begingroup$ @BenI. But that particular phrasing lacks a level of quotation. Correct would be, whether x differs from whether y. $\endgroup$
    – philipxy
    Commented May 1, 2020 at 0:06
  • 1
    $\begingroup$ @Flater, != is xor! The distinction between the return values of the bitwise and logical operators when applied to bit arrays, was out of scope. I'd be happy to provide a comprehensive answer if you asked for a deeper explanation. $\endgroup$
    – Steve
    Commented Jun 19, 2020 at 15:02
3
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ I liked your simple and easy to understand example! (+1) Sorry for editing your answer too much. I extracted my changes and made it another answer based on yours, as was suggested to me. I hope I have restored your text without mistake. $\endgroup$
    – Antares
    Commented May 3, 2020 at 5:31
  • $\begingroup$ @Antares it's all good. $\endgroup$ Commented May 3, 2020 at 5:34
2
$\begingroup$

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.

$\endgroup$
7
  • $\begingroup$ Love the last one, but 1 - 3 seem to be inclusive or, not xor. $\endgroup$
    – Buffy
    Commented Apr 23, 2020 at 14:49
  • $\begingroup$ How about "you will pass this class, or not". $\endgroup$
    – Buffy
    Commented Apr 23, 2020 at 14:52
  • $\begingroup$ @Buffy That's merely a Boolean, xor requires two Booleans to compare. $\endgroup$ Commented Apr 23, 2020 at 14:54
  • $\begingroup$ @GypsySpellweaver, how so? "You will pass this class or you will not pass this class". Clear xor. "You will pass this class or you won't be able to graduate". $\endgroup$
    – Buffy
    Commented Apr 23, 2020 at 15:04
  • $\begingroup$ Pass or not is a result. What are the two, exclusive conditions which determine the result. Pass not graduate is a single input noted. $\endgroup$ Commented Apr 23, 2020 at 15:12
2
$\begingroup$

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.

$\endgroup$
7
  • $\begingroup$ Actually, I'm not sure. I think most (or at least many) uses of or actually have an xor intent. "Pancakes or eggs?" The asker will be surprised if you say both. It generates a laugh. $\endgroup$
    – Buffy
    Commented Apr 24, 2020 at 20:47
  • $\begingroup$ @buffy "pancakes or eggs" is selection not the xor predicate. It seems that the same word is used for exclusive selection and inclusive predicate. $\endgroup$ Commented Apr 24, 2020 at 21:39
  • $\begingroup$ @Buffy Exactly! "Pancakes or eggs" is a perfect example. If you try to turn it to code, it becomes clear it's not analogous to pancakes != eggs, but instead it's branching. I don't think that the word "or" can, by itself, carry an xor relationship. $\endgroup$
    – Ben I.
    Commented Apr 25, 2020 at 13:31
  • $\begingroup$ I'm losing track of the argument here. It isn't a question about whether pancakes are eggs. It about whether "I'm eating" pancakes or eggs or maybe both. It isn't the "thing", but the action. "I'm eating pancakes" can be true or false. Etc. If I"m not making sense, then I don't really understand the meaning or intent of the OP here. $\endgroup$
    – Buffy
    Commented Apr 25, 2020 at 13:35
  • $\begingroup$ @Buffy The equivalent code is if (choosesPancakes) bringPancakes(); else if (choosesEggs) bringEggs(); But "both" makes no sense here, and in any case, there is nothing to indicate that both "both" and "neither" would somehow evaluate to false. Ctrl-alt-delor can correct me if I misunderstood, but I believe he wants a situation where the English "or" is evaluated along the lines of Java's ^ or !=, and where the full truth table for $xor$ holds true and makes sense. My contention is that those 4 truth table evaluations (00->0, 01->1, 10->1, 11->0) cannot be held by the word "or". $\endgroup$
    – Ben I.
    Commented Apr 25, 2020 at 16:54
2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ "So "all are different" and "all are same" are inverse to each other, logically." Watch out with your phrasing on the inverses. This is only correct for two elements, not for more. In the set 1,1,2, not all numbers are the same, but not all numbers are different either! Therefore, these two statements are not each other's inverse. $\endgroup$
    – Flater
    Commented Jun 19, 2020 at 14:54
1
$\begingroup$

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"

$\endgroup$
0
0
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ Example 3 is the only predicate, it is a contracted sentence, of I like cars that are fully blue or fully yellow yellow, and has the meaning I like cars that are fully yellow and cars that are fully blue. And there for has the meaning AND. Examples 1 and 2 are selection not a predicate (selection is usually mutually exclusive, but not in example 2) $\endgroup$ Commented Jun 19, 2020 at 15:47
  • $\begingroup$ @ctrl-alt-delor: Your assertion that semantical english unambiguously translates to logic simply does not hold true. "and" is used differently as and (I like cars and planes != I like vehicles that are both a car and a plane), and "or" is similarly used differently than or (cfr all the above examples) $\endgroup$
    – Flater
    Commented Jun 19, 2020 at 15:52
  • $\begingroup$ Did I assert that? Sorry for making such a suggestion. As for the 2nd bit, I don't grok it all, but your negative predicate is true of English and of logic. However I agree English is not as formal and unambiguous as logic. $\endgroup$ Commented Jun 19, 2020 at 16:02
-1
$\begingroup$

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.)

$\endgroup$
4
  • 1
    $\begingroup$ While this is a use of or in english, and it is exclusive. It is not an xor operation. Because it is not a predicate. It is a procedure. It is selection (a branch/if/conditional), not xor. $\endgroup$ Commented Apr 24, 2020 at 21:36
  • $\begingroup$ @ctrl-alt-delor, I don't see that statement as imperative. I see an assertion about the future. It can be tested after the person who made it returns from the animal shelter. If they return with one dog or one cat, then the assertion was true. If they come back with anything else, then the assertion was false. $\endgroup$ Commented Apr 28, 2020 at 13:22
  • $\begingroup$ @besmirched and if they comeback with both? $\endgroup$ Commented Apr 29, 2020 at 9:11
  • $\begingroup$ @ctrl-alt-delor Ha ha! While I still disagree with you about whether or not the example is "procedural," I guess I've added another example of how one must be careful when attempting to express the XOR idea in English. $\endgroup$ Commented Apr 29, 2020 at 11:12
-1
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ Zen moment: it is a comment and an answer. $\endgroup$
    – Ben I.
    Commented Apr 30, 2020 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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