Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code that return True if only one or two of three params are true

I need a code that return True if only one or two of three params are true

what is the shortest/best way?

like image 429
Erez Avatar asked Mar 17 '11 18:03

Erez


2 Answers

I'm addicted to this question!

bool MyFourthAnswer(bool a, bool b, bool c)
{
   return (a != b) || (b != c);
}
like image 69
Stuart Avatar answered Oct 29 '22 01:10

Stuart


Just check whether at least one of the values is set and not all three values are set:

bool result = (a | b | c) & !(a & b & c);
like image 45
Konrad Rudolph Avatar answered Oct 29 '22 00:10

Konrad Rudolph