Guest User

05.Bit Flipper

a guest
Apr 14th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 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. using System.Numerics;
  7.  
  8. class BitFlipper
  9. {
  10.     static void Main()
  11.     {
  12.         ulong input = ulong.Parse(Console.ReadLine());
  13.         string newstring = Convert.ToString((long)input, 2).PadLeft(64,'0');
  14.         char[] bits = newstring.ToCharArray();
  15.         for(int i = 0;i<62;i++)
  16.         {
  17.             if(bits[i] == '0' && bits[i+1] == '0' && bits[i+2] == '0')
  18.             {
  19.                 bits[i] = '1';
  20.                 bits[i+1] = '1';
  21.                 bits[i+2] = '1';
  22.                 if (i < 62)
  23.                 {
  24.                     i += 2;
  25.                 }
  26.             }
  27.             else if(bits[i] == '1' && bits[i + 1] == '1' && bits[i + 2] == '1')
  28.             {
  29.                 bits[i] = '0';
  30.                 bits[i + 1] = '0';
  31.                 bits[i + 2] = '0';
  32.                 if(i<62)
  33.                 {
  34.                     i += 2;
  35.                 }
  36.             }
  37.         }
  38.         string result = "";
  39.         foreach (char ch in bits)
  40.         {
  41.             result += ch;
  42.         }
  43.         ulong s = Convert.ToUInt64(result, 2);
  44.         Console.WriteLine(s);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment