mellowdeep

Bit Flipper

Dec 7th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class BitFlipper
  5. {
  6.     static void Main()
  7.     {
  8.         ulong input = ulong.Parse(Console.ReadLine());
  9.         string bit = Convert.ToString((long)input, 2).PadLeft(64,'0');
  10.         string result = string.Empty;
  11.        
  12.         for (int i = 0; i < bit.Length; i++)
  13.         {
  14.             string tempOne = bit[i].ToString();
  15.             string tempTwo = string.Empty;
  16.             string tempThree = string.Empty;
  17.  
  18.             if (i + 1 < bit.Length)
  19.             {
  20.                 tempTwo = bit[i + 1].ToString();
  21.             }
  22.             if (i + 2 < bit.Length)
  23.             {
  24.                tempThree = bit[i + 2].ToString();
  25.             }
  26.            
  27.             if (tempOne.Equals("1") && tempTwo.Equals("1") && tempThree.Equals("1"))
  28.             {
  29.                 result += "000";
  30.                 i += 2;
  31.                 continue;
  32.             }
  33.             else if ((tempOne.Equals("0") && tempTwo.Equals("0") && tempThree.Equals("0")))
  34.             {
  35.                 result += "111";
  36.                 i += 2;
  37.                 continue;
  38.             }
  39.             else
  40.             {
  41.                 result += bit[i];
  42.             }
  43.  
  44.         }
  45.         Console.WriteLine(Convert.ToUInt64(result,2));
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment