Skip to content

Instantly share code, notes, and snippets.

@mientjan
Last active January 12, 2016 11:51
Show Gist options
  • Save mientjan/e5a8006f1c5e5a98edcc to your computer and use it in GitHub Desktop.
Save mientjan/e5a8006f1c5e5a98edcc to your computer and use it in GitHub Desktop.
// A bitwise FlagMachine
// create a enum
enum PizzaIngredients {
BREAD = 1 << 0,
CHEESE = 1 << 1,
TOMATO = 1 << 2,
UNIONS = 1 << 3,
GARLIC = 1 << 4,
}
var pizzaMozzarella = new Flag<PizzaIngredients>(PizzaIngredients.BREAD | PizzaIngredients.TOMATO | PizzaIngredients.CHEESE);
var garlicBread = new Flag<PizzaIngredients>(PizzaIngredients.BREAD | PizzaIngredients.GARLIC);
var pizza = new Flag<PizzaIngredients>();
pizza.add( PizzaIngredients.BREAD | PizzaIngredients.TOMATO | PizzaIngredients.CHEESE );
pizza.equals(pizzaMozzarella); // true
pizza.add(garlicBread); // true
pizza.contains(garlicBread); // true
pizza.equals(pizzaMozzarella); // false because PizzaMozzarella contains no garlic
pizza.equals(pizzaMozzarella | PizzaIngredients.GARLIC); // true now it does
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment