Switch on bitwise enumeration

Question:

I have an function that fetches data, validates it against a set of user specified parameters and outputs if it is correct or not and based on that takes action, and it works just fine. As a further development of the function I’ve tried to move to a bitwise enumeration for the ‘flags’ but cannot get it to work like i want.

I want to switch through the enumeration and run the case for all possible ‘matches’ but i only get matches on the case when there is only a direct match on the value. Ie. when the value is 1 it runs the case ‘Milk’ but if the value is 3 it will not run the ‘Milk’ and the ‘Bread’ case … i kinda assume its trying to run the ‘Milk, Bread’ case.

Is there any easy way that I’ve failed to see around this? ><

Answer:

Yet another variant:

Output:

Source:

Switch on bitwise enumeration by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply