user69514
user69514

Reputation: 27629

Bitwise operation Java AND multiple booleans

this is my problem. I have three booleans which are options I can have. I can have multiple combinations with the three options:

i.e. no options (all false) option 1 only option 1 and option 2 option 1 and option 2 and option 3 option 2 only option 2 and option 3 option 3 only etc

I need to check all the combinations, but I don't want to write a tons of if else if statements.

Is there a way a can obtain what the results should be?

something like

result = option1 & option2 & option3

and then I can just go in a switch statement to process the correct combination

Let me know if you need a more detailed explanation. Thanks in advance.

p.s. what I am trying to do here is to avoid having so many if else if statements and make my code look cleaner and better designed. So if you can think of another way to do, I'd appreciate it.

Thanks

Upvotes: 1

Views: 2014

Answers (3)

Christian Semrau
Christian Semrau

Reputation: 9013

One way to get hold of all 8 cases in a switch is the following.

Convert the booleans to different int flags (values with only one bit set), combine these with bitwise OR, and then switch on the 8 possible values.

int combination = (option1 ? 1 : 0) | (option2 ? 2 : 0) | (option3 ? : 4 : 0);

switch(combination) {
case 0: // !1 && !2 && !3
  ...
break;
case 1: // 1 && !2 && !3
  ...
break;
case 2: // !1 && 2 && !3
  ...
break;
case 3: // 1 && 2 && !3
  ...
break;
case 4: // !1 && !2 && 3
  ...
break;
case 5: // 1 && !2 && 3
  ...
break;
case 6: // !1 && 2 && 3
  ...
break;
case 7: // 1 && 2 && 3
  ...
break;
}

With this approach, you can handle all 8 cases equally. But it will grow out of hands if there are more booleans added, because the number of combinations grows exponentially.

Upvotes: 1

s106mo
s106mo

Reputation: 1253

My solution does not work with booleans, but you could adapt it. For example the call of Option.CombinedOption.get(Option.ONE, Option.THREE) returns the enum CombinedOption.OPTION_1_3.

public enum Option {
ONE, TWO, THREE;

public enum CombinedOption {

    ALL_FASLSE(), OPTION_1(ONE), OPTION_1_2(ONE, TWO), OPTION_1_3(ONE,
            THREE), OPTION_1_2_3(ONE, TWO, THREE), OPTION_2(TWO), OPTION_2_3(
            TWO, THREE), OPTION_3(THREE);

    private Set<Option> keySet;

    private CombinedOption(Option... options) {
        if (options != null && options.length > 0)
            keySet = EnumSet.copyOf(Arrays.asList(options));
        else
            keySet = EnumSet.noneOf(Option.class);

    }

    private static final Map<Set<Option>, CombinedOption> optionMapping = new HashMap<Set<Option>, CombinedOption>();

    static {
        for (CombinedOption combined : CombinedOption.values()) {
            optionMapping.put(combined.keySet, combined);
        }
    }

    public static CombinedOption get(Option... options) {
        Set<Option> keySet;

        if (options != null && options.length > 0)
            keySet = EnumSet.copyOf(Arrays.asList(options));
        else
            keySet = EnumSet.noneOf(Option.class);

        return optionMapping.get(keySet);
    }
}

}

Upvotes: 0

Guvante
Guvante

Reputation: 19203

You could generate a Karnaugh_map for each possible outcome, using its rules you can simply the logic for a particular set of conditions down to the minimum potentially.

However I think it is best to go for clarity, try following the logic flow for why certain branches are taken. If it is too convoluted maybe rethinking the situation is in order.

Upvotes: 2

Related Questions