Advertisement
Guest User

Bit Builder

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