Christopher Benson-Manica writes:
[re: introducing a logical XOR operator]
Because ^ is good enough, given the fact that a logical XOR operator
couldn't have any shortcircuiting semantics. !a ^ !b yields exactly
the same result as the hypothetical ^^ operator.
Possibly dumb, and possibly OT, question: What if 'a' and/or 'b' are
doubles with the IEEE value NaN, that screws up comparisons? Does
If a and b are already
logical expressions (i.e. expressions that evaluate to 0 or 1), a ^ b is
enough.
Are you still convinced that it would be useful?
Well, a logical xor operator wouldn't add anything to the capabilities
of the language, but then neither does having a special operator for
subtraction. I have, on occasion, run across situations where an XOR
operator would have been useful. Christopher mentioned command-line
argument parsing, such as:
[...]
switch (argv
[j]) {
case 'K': KNRStyle = 1; break;
case 'A': AnsiStyle = 1; break;
}
[...]
if (KNRStyle ^^ AnsiStyle)
process(infp, outfp);
else
do_error("You must specify exactly one of -K, -A!\n");
Here the ^^ operator would capture the spirit of the condition better
than the != alternative, as well as being slightly more robust. The
suggested alternative,
if (!KNRStyle != !AnsiStyle)
is IMO so obfuscated as to be actually worse than simply enumerating
the cases:
if (KNRStyle && !AnsiStyle || AnsiStyle && !KNRStyle)
I also submit for consideration, as I've done before, the usefulness
of logical operators when dealing with pointers:
struct btnode {
int value;
struct btnode *left;
struct btnode *right;
};
int bt_IsUselessBranch(struct btnode *p)
{
return (p->value == 0) && (p->left ^^ p->right);
}
versus
return (p->value == 0) && (!(p->left) != !(p->right));
Of course, both cases are pretty ugly, here.