10

E.g.,

if (bool1 ^ bool2 ^ bool3 ^ bool4)
{
    // Do whatever
}

It should execute only if exactly one of the conditions is met.

Boann
  • 48,794
  • 16
  • 117
  • 146
Beta Bet
  • 103
  • 5
  • 4
    It's possible to write that, but it may not do what you want. Build a truth table to show you the outcomes of all 16 combinations to decide if that's what you want. Your statement _"only if one of the conditions is met"_ is ambiguous. As written, a simple inclusive-or would suffice. Do you mean _"only if exactly ONE of the conditions is met"_? – Jim Garrison Feb 25 '17 at 19:01
  • 1
    Why don't you try it, and tell us? Or come back if you have a question after trying. – Elliott Frisch Feb 25 '17 at 19:02
  • 1
    @Esko, it is not a duplicate. – Grzegorz Górkiewicz Feb 25 '17 at 19:07
  • ...well, yeah. I admit clicking the wrong thing. BRB :) – Esko Feb 25 '17 at 19:10
  • 1
    @beta see http://stackoverflow.com/a/21384722/44523 – Esko Feb 25 '17 at 19:10
  • I did try it and got a negative result, hence my question. It seems to be working elsewhere in the code though. @JimGarrison you are right, I just edited it. – Beta Bet Feb 25 '17 at 19:15
  • @esko thanks, helps with understanding – Beta Bet Feb 25 '17 at 19:15
  • In a similar theme: https://stackoverflow.com/questions/3076078/check-if-at-least-two-out-of-three-booleans-are-true – Boann Feb 26 '17 at 14:43

3 Answers3

10

Add the bools together as integers and check if they equal 1.

In a language where casting from a boolean to an integer doesn't work, such as Java, the more long winded option is:

if ((bool1 ? 1 : 0) + (bool2 ? 1 : 0) + (bool3 ? 1 : 0) + (bool4 ? 1 : 0) == 1) {
    // only runs when one of bool 1-4 is true
}

However, in other languages where casting a boolean to an integer is valid, you can do the following:

if ((int)(bool1) + (int)(bool2) + (int)(bool3) + (int)(bool4) == 1) {
    // only runs when one of bool 1-4 is true
}
Liam Gray
  • 1,089
  • 9
  • 16
  • Since this was tagged `java`, "a language where casting from a boolean to an integer doesn't work" is a given. – Lew Bloch Feb 25 '17 at 23:09
  • @LewBloch, the answerer probably does not know Java well and with this sentence he makes his answer general so that it can be applied in many languages. – Grzegorz Górkiewicz Feb 26 '17 at 14:47
  • I answered the question without testing it in Java, realised casting didn't work, then gave the other solution. I'll update the answer though! – Liam Gray Feb 26 '17 at 14:49
2

As an alternative, here is a stream-based solution:

boolean b0 = false;
boolean b1 = false;
boolean b2 = true;
boolean b3 = false;

boolean result = Stream.of(b0, b1, b2, b3)
    .mapToInt(b -> b ? 1 : 0)
    .sum() == 1;

System.out.println(result);

It has the advantage that it can easily be applied to 3 or 5 boolean values, or to generically implement a boolean exactlyOne(boolean ... b) method.

Marco13
  • 53,703
  • 9
  • 80
  • 159
0

Your expression won't work because it means

(((bool1 ^ bool2) ^ bool3) ^ bool4)

Suppose bool1 = bool2 = bool3 = true and bool4 = false. Overal expression will give true.

You probably meant some XOR generalization, but there are several of them. One may work as you said, but more often it means odd number of ones. Anyway, most programming languages do not have it out of the box.

Dims
  • 47,675
  • 117
  • 331
  • 600