Advertisement
Guest User

5. Game of Bits

a guest
Mar 30th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication5
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. long number = long.Parse(Console.ReadLine());
  14.  
  15. while (true)
  16. {
  17. string command = Console.ReadLine();
  18. if (command == "Game Over!")
  19. {
  20. break;
  21. }
  22.  
  23. long num = 0;
  24. int numPos = 0;
  25. string numbrAsString = Convert.ToString(number, 2);
  26. for (int pos = 0; pos < numbrAsString.Length; pos++)
  27. {
  28. long mask = 0;
  29. string maskAsString = new string('1', pos);
  30. if (pos != 0)
  31. {
  32. mask = Convert.ToInt64(maskAsString, 2);
  33. }
  34. long rightBits = num & mask;
  35.  
  36. int tmp = pos + 1;
  37. if (command == "Odd")
  38. {
  39. if (tmp % 2 != 0)
  40. {
  41. long bit = 1 & number >> pos;
  42. //num = bit + num;
  43. if (bit == 1)
  44. {
  45. num = num >> numPos;
  46. num = num << (numPos + 1);
  47. num = num | ((long)1 << numPos);
  48. num = num | rightBits;
  49. numPos++;
  50. //num = num << 1;
  51. //num = num | 1;
  52. }
  53. else
  54. {
  55. num = num >> numPos;
  56. num = num << (numPos + 1);
  57. //num = num | ((long)1 << numPos);
  58. num = num | rightBits;
  59. numPos++;
  60. //num = num << 1;
  61. }
  62. }
  63. }
  64. else
  65. {
  66. if (tmp % 2 == 0)
  67. {
  68. long bit = 1 & number >> pos;
  69. if (bit == 1)
  70. {
  71. num = num >> numPos;
  72. num = num << (numPos + 1);
  73. num = num | ((long)1 << numPos);
  74. num = num | rightBits;
  75. numPos++;
  76. //num = num << 1;
  77. //num = num | 1;
  78. }
  79. else
  80. {
  81. num = num >> numPos;
  82. num = num << (numPos + 1);
  83. //num = num | ((long)1 << numPos);
  84. num = num | rightBits;
  85. numPos++;
  86. //num = num << 1;
  87. }
  88. }
  89. }
  90. }
  91. number = num;
  92. }
  93.  
  94. int bitCount = 0;
  95. string numAsString = Convert.ToString(number, 2);
  96. for (int i = 0; i < numAsString.Length; i++)
  97. {
  98. if (numAsString[i] == '1')
  99. {
  100. bitCount++;
  101. }
  102. }
  103.  
  104. Console.WriteLine("{0} -> {1}", number, bitCount);
  105.  
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement