Live data from Hacker News

The Three Ways of XOR

horia141.com

1–10 of 69 posts

Re: The Three Ways of XOR

#2
> most programming languages don’t have an explicit “logical operator” for it

I guess the author never taught of != . The only thing to be careful with is that it doesn't implicitly convert arguments to boolean, so expressions like " != (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

#3
post #2

> most programming languages don’t have an explicit “logical operator” for it I guess the author never taught of != . The only thing to be careful with is that it doesn't implicitly convert arguments to boolean, so expressions like " != (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'v…

Is "bool(a1) + ... + bool(aN) == M" not just "a1 || a2...||an"?

Re: The Three Ways of XOR

#4
post #2

> most programming languages don’t have an explicit “logical operator” for it I guess the author never taught of != . The only thing to be careful with is that it doesn't implicitly convert arguments to boolean, so expressions like " != (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'v…

Is "bool(a1) + ... + bool(aN) == M" not just "a1 || a2...||an"?

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

#5
post #2

> most programming languages don’t have an explicit “logical operator” for it I guess the author never taught of != . The only thing to be careful with is that it doesn't implicitly convert arguments to boolean, so expressions like " != (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'v…

Is "bool(a1) + ... + bool(aN) == M" not just "a1 || a2...||an"?

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

#6
Lovely article, in particular the small but deep excursion into AI history and the AI winter after the publication of Minsky/Papert's "Perceptrons" (though it was 70's, not 80's).

Wonder when the current AI summer will come to an end...

Re: The Three Ways of XOR

#8
post #5

Earlier quoted context omitted.

Is "bool(a1) + ... + bool(aN) == M" not just "a1 || a2...||an"?

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".

So your + operator implicitly casts booleans to an integer type, with false=0 and true=1?

Re: The Three Ways of XOR

#10
post #5

Earlier quoted context omitted.

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".

So your + operator implicitly casts booleans to an integer type, with false=0 and true=1?

In c++, yes. Not every language will do that though.
Post reply on HN