Logo of inline category

3-way XOR

Very occasionally, I stumble across a rare logic condition: a 3-way (or even n-way) XOR. There might be a better name for it, but it essentially is this problem:
You want a condition with to be true iff of variables A, B, C, … only one is true.

We could start nesting if-clauses like this:

but of course this is both needlessly long and hard to maintain, especially if you have more or even an unknown amount of variables.

A simpler if somewhat hacky way to solve the problem is to take advantage of the fact that true-values can be cast to integers, at least in Python and C++ (and many other languages).
This way we can simply write the following code:

Just be aware, especially in C++ and other typed languages, that implicit conversion to an integer might not always work as expected. If in doubt (better still: always) use explicit casting.