Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Why isn't there a logical XOR operator?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Arthur J. O'Dwyer, post: 1699259"] [re: introducing a logical XOR 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 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[i][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. But I haven't yet come up with a good example. ;-) And while I think it would have been cool if C had been designed with a ^^ operator from the start, IMHO it's much too late now to try adding it to the standard language. C doesn't need any more syntax, and even if it did, I don't think the new syntax would find many willing users. A new version of C would IMHO suffer the same problems as C99 still suffers w.r.t. market share, only worse. Now, a *new* language... :-) -Arthur[/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Why isn't there a logical XOR operator?
Top