Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum Options : int
- {
- PrintA = 1,
- PrintB = 2,
- PrintC = 4,
- PrintD = 8
- }
- public enum Options : int
- {
- PrintA = 1,
- PrintB = 2,
- PrintC = 4,
- PrintD = 8
- }
- static bool GetBit(int number, int posLeftRight)
- {
- return (number & (1 << posLeftRight)) != 0;
- }
- static void Main()
- {
- // Using OR to "add" options up
- int x = Options.PrintA | Options.PrintB | Options.PrintD;
- /*
- x is now 11 (1+2+8) because:
- 00000001 (1)
- | 00000010 (2)
- = 00000011 (3)
- | 00001000 (8)
- = 00001011 (11)
- */
- if(GetBit(x, 0))
- {
- // the if above checks this bit:
- // 00000000
- // ^
- Console.WriteLine("A");
- }
- if(GetBit(x, 1))
- {
- // the if above checks this bit:
- // 00000000
- // ^
- Console.WriteLine("B");
- }
- if(GetBit(x, 2))
- {
- // the if above checks this bit:
- // 00000000
- // ^
- Console.WriteLine("C");
- }
- if(GetBit(x, 3))
- {
- // the if above checks this bit:
- // 00000000
- // ^
- Console.WriteLine("D");
- }
- }
Add Comment
Please, Sign In to add comment