Item | Decimal | Binary |
---|---|---|
Pop | 1 | 0000001 |
Chips | 2 | 0000010 |
CandyBar | 4 | 0000100 |
Gyro | 8 | 0001000 |
Shwarma | 16 | 0010000 |
Pizza | 32 | 0100000 |
Pop | 64 | 1000000 |
Now that we have our values if we where to have a bitwise OR on any combination of products lets, do pop and chips we'd get: 01 or 10 = 11; 11 being the binary equivalent of 3. the significance of this is as follows
using System;
namespace EnumExample
{
class Program
{
[Flags]
enum Food : short
{
Pop = 1, Chips = 2, CandyBar = 4,
Gyro = 8, Shwarma = 16, Pizza = 32, Spuds = 64
}
static void Main(string[] args)
{
var f1 = Food.Chips;//01
var f2 = Food.Pop; //10
Console.WriteLine($"{f1} has a value of {(short)f1}");
Console.WriteLine();
Console.WriteLine($"{f2} has a value of {(short)f2}");
Console.WriteLine();
Console.WriteLine($"{f1 | f2} have a value of {(short)(f1 |
f2)}");
}
}
}
if we didn't decorate our enum with the [Flags] attribute, when we tried to do a bit-wise OR on f1 and f2 we'd just get the numeric value of three, but because we used the [Flags] attribute we see Pop, Chips instead.