How to compactly write "Exclusive OR"?

How do I implement an exclusive or? In most languages, this construct is not implemented at the language level. Therefore, we have to write non-obvious constructions of the form:

!a && b || b && !a

This can be wrapped in a function, of course:

xor(a, b)

But we can not exclude the human factor, which can often lead to constructions of the form:

!variable.very_long_name_field_1.very_long_subname_field && variable.very_long_name_field_1.very_long_subname_field_a || variable.very_long_name_field_1.very_long_subname_field && !variable.very_long_name_field_1.very_long_subname_field_a

As an answer, give a compact example of calculating XOR (excluding or).

Author: hedgehogues, 2020-04-19

3 answers

For boolean values, in go, the answer is:

if boolA != boolB {

}

The result of the comparison is true only if one of the operands is true and the other is false, i.e. they are not equal to each other. This is how xor works.

Https://stackoverflow.com/a/27211124

 3
Author: zed, 2020-04-19 08:22:35

For python:

bool(a) != bool(b)

Response A. Coady

 4
Author: Кирилл Малышев, 2020-04-19 06:30:24
bool(a) ^ bool(b)

When a and / or b are already of the bool type, do not - of course-apply the bool() function to it, but do not forget to use brackets, for example

(5 > 4) ^ (1 > 7)     # True
 4
Author: MarianD, 2020-04-22 18:13:29