BorisSimeonov

Bit Inverter

Nov 7th, 2014
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System;
  2.  
  3. class BitInverter
  4. {
  5.     static void Main()
  6.     {
  7.         int n = int.Parse(Console.ReadLine());
  8.         int step = int.Parse(Console.ReadLine());
  9.         short[] num = new short[n];
  10.         int pos = 7;
  11.         for (int index = 0; index < n; index++)
  12.         {
  13.             num[index] = byte.Parse(Console.ReadLine());
  14.             for ( ; ; )
  15.             {
  16.                 if (pos >= 0)
  17.                 {
  18.                     num[index] = changeValue(num[index], pos);
  19.                     pos -= step;
  20.                 }
  21.                 else
  22.                 {
  23.                     pos += 8;
  24.                     break;
  25.                 }
  26.             }
  27.         }
  28.         foreach (int x in num)
  29.         {
  30.             Console.WriteLine(x);
  31.         }
  32.     }
  33.  
  34.     private static short changeValue(short num, int pos)
  35.     {
  36.         if ((num & (1 << pos)) != 0)
  37.         {
  38.             num -= (short)(1 << pos);
  39.         }
  40.         else
  41.         {
  42.             num += (short)(1 << pos);
  43.         }
  44.         return num;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment