Why isn't there a logical XOR operator?

  • Thread starter Christopher Benson-Manica
  • Start date
C

CBFalconer

Larry said:
Maybe "average human beings" shouldn't try to write or
maintain non-trivial C programs.

That expression is true if an odd number of a, b, c, d,
and e are true.

- Larry

#define _(x) !!(x)

now your expression, including ensuring logical values, becomes:

_(a) ^ _b) ^ _(c) ^ _(d) ^ _(d) ^ _(e)
or
a ^ b ^ c ^ d ^ e

when the values are preknown to be logical, i.e. 0 or 1. In that
case you can also use:

(a + b + c + d + e) & 1

(and I believe the single _ to be a valid identifier)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top