Advertisement
dimipan80

Exam 8. Bit Flipper

Jun 22nd, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Threading;
  4.  
  5. public class BitFlipper
  6. {
  7.     public static void Main()
  8.     {
  9.         Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  10.         checked
  11.         {
  12.             ulong number = ulong.Parse(Console.ReadLine());
  13.  
  14.             const ulong SEQ3BITS = 7;
  15.             for (int position = 63; position >= 2; position--)
  16.             {
  17.                 ulong bitMask = SEQ3BITS << (position - 2);
  18.                 ulong bitValue = (number & bitMask) >> (position - 2);
  19.                 if (bitValue == 0)
  20.                 {
  21.                     number |= bitMask;
  22.                     position -= 2;
  23.                 }
  24.                 else if (bitValue == SEQ3BITS)
  25.                 {
  26.                     number &= ~bitMask;
  27.                     position -= 2;
  28.                 }
  29.             }
  30.  
  31.             Console.WriteLine(number);
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement