Advertisement
Guest User

Untitled

a guest
Dec 21st, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 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 position = int.Parse(positionS);
  20. if(command=="flip")
  21. {
  22. number = number ^ (1 << position);
  23. }
  24. if(command=="insert")
  25. {
  26. for (int i = 0; i <= 31; i++)
  27. {
  28. if (i == position)
  29. {
  30. result = result | (1 << position);
  31. i++;
  32. }
  33. int bit = (number >> i) & 1;
  34. result = result | bit << i;
  35. }
  36. number = result;
  37. }
  38. if(command =="remove")
  39. {
  40. for (int i = 0; i < 32; i++)
  41. {
  42. if (i == position)
  43. {
  44. continue;
  45. }
  46. result = result >> 1;
  47. int bit = (number >> i) & 1;
  48. result = result | bit << 30;
  49. }
  50. number = result;
  51. }
  52. positionS = Console.ReadLine();
  53. if (positionS == "quit")
  54. {
  55. break;
  56. }
  57. command = Console.ReadLine();
  58.  
  59. }
  60. Console.WriteLine(number);
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement