Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 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 bitbuilder
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int number = int.Parse(Console.ReadLine());
  14. string positionS = Console.ReadLine();
  15. string command = Console.ReadLine();
  16. while (command != "quit")
  17. {
  18. int result = 0;
  19. int bit = 0;
  20. int position = int.Parse(positionS);
  21. if (command == "flip")
  22. {
  23. number = number ^ (1 << position);
  24. }
  25. if (command == "insert")
  26. {
  27. for (int i = 0; i <= 31; i++)
  28. {
  29. if (i == position)
  30. {
  31. result = result | (1 << position);
  32. for (int j = i, p=i+1; j <= 31;++p, j++)
  33. {
  34. if(p==32)
  35. {
  36. p--;
  37. }
  38. bit = (number >> j) & 1;
  39. result = result | bit << p;
  40. //Console.WriteLine(Convert.ToString(result, 2).PadLeft(16, '0'));
  41. }
  42. break;
  43. }
  44. bit = (number >> i) & 1;
  45. result = result | bit << i;
  46. //Console.WriteLine(Convert.ToString(result, 2).PadLeft(16, '0'));
  47. }
  48. number = result;
  49. }
  50. if (command == "remove")
  51. {
  52. for (int i = 0; i <= 31; i++)
  53. {
  54. if (i == position)
  55. {
  56. for (int j = i,p=i+1; j <= 31; ++p,j++)
  57. {
  58. if (p == 32)
  59. {
  60. p--;
  61. }
  62. bit = (number >> p) & 1;
  63. result = result | bit << j;
  64. //Console.WriteLine(Convert.ToString(result, 2).PadLeft(16, '0'));
  65. }
  66. break;
  67. }
  68. bit = (number >> i) & 1;
  69. result = result | bit << i;
  70.  
  71. //Console.WriteLine(Convert.ToString(result, 2).PadLeft(16, '0'));
  72. }
  73. number = result;
  74. }
  75. positionS = Console.ReadLine();
  76. if (positionS == "quit")
  77. {
  78. break;
  79. }
  80. command = Console.ReadLine();
  81.  
  82. }
  83. //Console.WriteLine(Convert.ToString(number, 2).PadLeft(16, '0'));
  84. Console.WriteLine(number);
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement