News for hackers. Stuff that matters.
Re:The Three Ways of XOR by ambrop7 on Sunday February 12, 2017 @05:13PM (#13631305)
> most programming languages don’t have an explicit “logical operator” for it

I guess the author never taught of <boolean> != <boolean>. The only thing to be careful with is that it doesn't implicitly convert arguments to boolean, so expressions like "<boolean> != (flags & Flag)" will go wrong without an explicit conversion "bool(flags & Flag)" or equivalent expression like "((flags & Flag) != 0)".

And let's not forget about the friend, ==. I've more than once seen code like "(a && b) || (!a && !b)".

A similar interesting pattern many don't think of is "bool(a1) + ... + bool(aN) == M" (particularly with M==1) and instead we see unreadable monstrosities :)

Re:The Three Ways of XOR by im3w1l on Monday February 13, 2017 @12:49AM (#13633385)
If you only want to convert the second argument you can use the ==! operator ;)
Re:The Three Ways of XOR by wruza on Monday February 13, 2017 @11:27AM (#13636474)
C has so many wonderful operators, why don't people use them?

  while (x --> 0) x goes to zero;
  while (0 <---- x) goes much faster on some compilers (ymmv);
Re:The Three Ways of XOR by andreareina on Monday February 13, 2017 @02:38AM (#13633721)
!!a != !!b also works, but that's unwieldy.

I think maybe part of the reason is that usually you're also doing something with the value of the lhs or rhs, so you end up having to separate the cases anyway:

    if (a && !b) { frob(a); }
    else if (b && !a) { twiddle(b); }
    else { panic(); }
Re:The Three Ways of XOR by caf on Monday February 13, 2017 @08:06AM (#13634916)
You don't need to double-negate,

  !a != !b
is sufficient.
Re:The Three Ways of XOR by TazeTSchnitzel on Sunday February 12, 2017 @07:22PM (#13632097)
> The only thing to be careful with is that it doesn't implicitly convert arguments to boolean

Well, you could also use bitwise XOR in that case.

Re:The Three Ways of XOR by kevinwang on Sunday February 12, 2017 @05:30PM (#13631413)
Is "bool(a1) + ... + bool(aN) == M" not just "a1 || a2...||an"?
Re:The Three Ways of XOR by kmill on Sunday February 12, 2017 @05:35PM (#13631446)
No, it's true when exactly M are true. "a1 || a2 || ... || an" is true when at least one is true.

The latter is the same as "a1 + ... + an == 1 || a1 + ... + an == 2 || ... || a1 + ... + an == n".

Re:The Three Ways of XOR by Ded7xSEoPKYNsDd on Sunday February 12, 2017 @06:01PM (#13631649)
So your + operator implicitly casts booleans to an integer type, with false=0 and true=1?
Re:The Three Ways of XOR by kmill on Sunday February 12, 2017 @06:26PM (#13631811)
What you are saying is the only way I can imagine to interpret what I wrote, if one assumes I was intending to make any sense.

By the way, in Python, bool is a subtype of int (i.e., instanceof(True,int) == True), and True + True == 2. C is the same, and in Javascript booleans are converted to integers for arithmetic operations.

Re:The Three Ways of XOR by OJFord on Sunday February 12, 2017 @07:52PM (#13632253)
> What you are saying is the only way I can imagine to interpret what I wrote

One might imagine it (without experience, or not thinking, of any particular programming language) as being a logical OR on Booleans - which in EE at least is frequently written '+'.

Re:The Three Ways of XOR by simcop2387 on Sunday February 12, 2017 @06:18PM (#13631765)
In c++, yes. Not every language will do that though.
Re:The Three Ways of XOR by ambrop7 on Sunday February 12, 2017 @05:33PM (#13631435)
Obviously no, the first (for M==1) means exactly one is true, the latter at least one is true. But a1||...||aN is equivalent to bool(a1)+...+bool(aN)>0 (assuming no integer overflow :).
Re:The Three Ways of XOR by Existenceblinks on Sunday February 12, 2017 @09:48PM (#13632782)
> "(a && b) || (!a && !b)"

It is XNOR, not XOR (no pun intended)

Boolean algebra laws excellently help if there are many input aN. I used to use it to simplify legacy ruby codes written by c-style programmer (you can imagine how the conditions look like)