iliya87

05.BitBuilder

Mar 25th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class BitBuilder
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         long number = long.Parse(Console.ReadLine());
  9.         string command = Console.ReadLine();
  10.  
  11.         while (command != "quit")
  12.         {
  13.             int position = int.Parse(command);
  14.             command = Console.ReadLine();
  15.  
  16.             long mask = 0;                                    //  Правим Маска и за дазапазим битовете  
  17.                                                               //      
  18.             for (int i = 0; i < position; i++)                //  
  19.             {                                                  //            
  20.                 mask <<= 1;                                    //
  21.                 mask |= 1;                                    //      
  22.             }
  23.  
  24.             long rightBits = number & mask;                     //
  25.  
  26.             switch (command)
  27.             {
  28.                 case "flip":
  29.                     number ^= 1 << position;
  30.                 break;
  31.  
  32.                 case "remove":
  33.                     number >>= position + 1;
  34.                     number <<= position;
  35.                     number |= rightBits;
  36.                 break;
  37.  
  38.                 case "insert":
  39.                 number >>= position;
  40.                 number <<= position + 1;
  41.                 number |= (long)1 << position;
  42.                 number |= rightBits;
  43.                 break;
  44.  
  45.             }
  46.             command = Console.ReadLine();
  47.         }
  48.         Console.WriteLine(number);
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment