Advertisement
dimipan80

Exam 4. Catch The Bits

Jul 11th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. namespace _5.CatchTheBits
  2. {
  3.     using System;
  4.  
  5.     public class CatchTheBits
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             checked
  10.             {
  11.                 int count = int.Parse(Console.ReadLine());
  12.                 int step = int.Parse(Console.ReadLine());
  13.  
  14.                 int bitCounter = 0;
  15.                 int positionInSequence = 0;
  16.                 int bitSequence = 0;
  17.                 for (int i = 0; i < count; i++)
  18.                 {                    
  19.                     byte numBytes = byte.Parse(Console.ReadLine());                    
  20.                     for (int currentBit = 7; currentBit >= 0; currentBit--)                  
  21.                     {                        
  22.                         if ((step == 1 && positionInSequence > 0) || (positionInSequence % step) == 1)
  23.                         {                                                        
  24.                             int bitMask = 1 << currentBit;
  25.                             int bitValue = (numBytes & bitMask) >> currentBit;
  26.                             bitSequence <<= 1;
  27.                             bitSequence |= (byte)bitValue;
  28.                             bitCounter++;
  29.                             if (bitCounter == 8)
  30.                             {
  31.                                 Console.WriteLine(bitSequence);
  32.                                 bitSequence = 0;
  33.                                 bitCounter = 0;
  34.                             }
  35.                         }
  36.  
  37.                         positionInSequence++;
  38.                     }
  39.                 }
  40.  
  41.                 if (bitCounter > 0)
  42.                 {
  43.                     bitSequence <<= 8 - bitCounter;
  44.                     Console.WriteLine(bitSequence);
  45.                 }
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement