Advertisement
ivanov_ivan

BitFlipper

Dec 12th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.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 BitFlipper
  8. {
  9.     class BitFlipper
  10.     {
  11.         public static string ConvertToBinary(ulong value)
  12.         {
  13.             if(value == 0)
  14.                 return "0";
  15.             System.Text.StringBuilder b = new System.Text.StringBuilder();
  16.             while(value != 0)
  17.             {
  18.                 b.Insert(0 , ( ( value & 1 ) == 1 ) ? '1' : '0');
  19.                 value >>= 1;
  20.             }
  21.             return b.ToString();
  22.         }
  23.         static void Main()
  24.         {
  25.             ulong input = ulong.Parse(Console.ReadLine());
  26.  
  27.             StringBuilder  bitsHack = new StringBuilder(ConvertToBinary(input).PadLeft(64,'0'));
  28.             bitsHack.Replace("111" , "222");
  29.             bitsHack.Replace("000" , "111");
  30.             bitsHack.Replace("222" , "000");
  31.  
  32.             ulong output = Convert.ToUInt64(bitsHack.ToString(),2);
  33.             Console.WriteLine(output);
  34.  
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement