Guest User

Untitled

a guest
Jan 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. public enum Options : int
  2. {
  3. PrintA = 1,
  4. PrintB = 2,
  5. PrintC = 4,
  6. PrintD = 8
  7. }
  8.  
  9. public enum Options : int
  10. {
  11. PrintA = 1,
  12. PrintB = 2,
  13. PrintC = 4,
  14. PrintD = 8
  15. }
  16.  
  17. static bool GetBit(int number, int posLeftRight)
  18. {
  19. return (number & (1 << posLeftRight)) != 0;
  20. }
  21.  
  22. static void Main()
  23. {
  24. // Using OR to "add" options up
  25. int x = Options.PrintA | Options.PrintB | Options.PrintD;
  26. /*
  27. x is now 11 (1+2+8) because:
  28. 00000001 (1)
  29. | 00000010 (2)
  30. = 00000011 (3)
  31. | 00001000 (8)
  32. = 00001011 (11)
  33. */
  34. if(GetBit(x, 0))
  35. {
  36. // the if above checks this bit:
  37. // 00000000
  38. // ^
  39. Console.WriteLine("A");
  40. }
  41. if(GetBit(x, 1))
  42. {
  43. // the if above checks this bit:
  44. // 00000000
  45. // ^
  46. Console.WriteLine("B");
  47. }
  48. if(GetBit(x, 2))
  49. {
  50. // the if above checks this bit:
  51. // 00000000
  52. // ^
  53. Console.WriteLine("C");
  54. }
  55. if(GetBit(x, 3))
  56. {
  57. // the if above checks this bit:
  58. // 00000000
  59. // ^
  60. Console.WriteLine("D");
  61. }
  62. }
Add Comment
Please, Sign In to add comment