Advertisement
fbinnzhivko

05.00.Bit Builder

Mar 17th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System;
  2. class BitBuilder
  3. {
  4.     static void Main()
  5.     {
  6.         long number = Convert.ToInt64(Console.ReadLine());
  7.  
  8.         string action = "";
  9.         int position = 0;
  10.  
  11.         while (true)
  12.         {
  13.             long mask = 0;
  14.  
  15.             string positionString = Console.ReadLine();
  16.  
  17.             if (positionString == "quit")
  18.             {
  19.                 Console.WriteLine(number);
  20.                 return;
  21.             }
  22.  
  23.             position = Convert.ToInt32(positionString);
  24.             action = Console.ReadLine();
  25.  
  26.             string maskAsString = new string('1', position);
  27.  
  28.             if (position != 0)
  29.             {
  30.                 mask = Convert.ToInt64(maskAsString, 2);
  31.             }
  32.  
  33.             if (action == "flip")
  34.             {
  35.                 number = number ^ (1 << position);
  36.             }
  37.  
  38.             long rightBits = number & mask;
  39.  
  40.             if (action == "insert")
  41.             {
  42.                 number = number >> position;
  43.                 number = number << (position + 1);
  44.                 number = number | ((long)1 << position);
  45.                 number = number | rightBits;
  46.             }
  47.  
  48.             if (action == "remove")
  49.             {
  50.                 number = number >> (position + 1);
  51.                 number = number << position;
  52.                 number = (long)number | rightBits;
  53.             }
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement