iliya87

05.BitsKiller

Mar 26th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System;
  2.  
  3. class BitsKiller
  4. {
  5.     static void Main()
  6.     {
  7.         int n = int.Parse(Console.ReadLine());
  8.         int step = int.Parse(Console.ReadLine());
  9.  
  10.         int index = 0;
  11.         int outputBits = 0;
  12.         int outputBitsCount = 0;
  13.         for (int i = 0; i < n; i++)
  14.         {
  15.             int num = int.Parse(Console.ReadLine());
  16.             for (int bitIndex = 7; bitIndex >= 0; bitIndex--)
  17.             {
  18.                 if (!((index % step == 1) || (step == 1 && index > 0)))
  19.                 {
  20.                     int bitValue = (num >> bitIndex) & 1;
  21.                     // Console.WriteLine("bit : " + bitValue);
  22.                     outputBits = outputBits << 1;
  23.                     outputBits = outputBits | bitValue;
  24.                     outputBitsCount++;
  25.                     if (outputBitsCount == 8)
  26.                     {
  27.                         // We have 8 bits (1 byte) --> flush it to the output
  28.                         Console.WriteLine(outputBits);
  29.                         outputBits = 0;
  30.                         outputBitsCount = 0;
  31.                     }
  32.                 }
  33.                 index++;
  34.             }
  35.         }
  36.  
  37.         if (outputBitsCount > 0)
  38.         {
  39.             // We have a few bits left --> add trailing zeroes and flush them to the output
  40.             outputBits = outputBits << (8 - outputBitsCount);
  41.             Console.WriteLine(outputBits);
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment