Advertisement
dimipan80

Exam 2. Bits Up

Jun 6th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. namespace _5.BitsUp
  2. {
  3.     using System;
  4.  
  5.     public class SetTo1TheBitsAtGivenPositionsInByteSequence
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             checked
  10.             {
  11.                 int countN = int.Parse(Console.ReadLine());
  12.                 int step = int.Parse(Console.ReadLine());
  13.                 byte[] bytes = ReadNumbersFromInputAndWriteThemInArray(countN);
  14.                 int indexInSequence = 0;
  15.                 for (int i = 0; i < bytes.Length; i++)
  16.                 {                    
  17.                     byte num = bytes[i];
  18.                     for (int bit = 7; bit >= 0; bit--)
  19.                     {
  20.                         if ((indexInSequence % step == 1) || (step == 1 && indexInSequence > 0))
  21.                         {
  22.                             num |= (byte)(1 << bit);
  23.                         }
  24.  
  25.                         indexInSequence++;
  26.                     }
  27.  
  28.                     Console.WriteLine(num);
  29.                 }
  30.             }
  31.         }
  32.  
  33.         private static byte[] ReadNumbersFromInputAndWriteThemInArray(int count)
  34.         {
  35.             byte[] arr = new byte[count];
  36.             for (int i = 0; i < arr.Length; i++)
  37.             {
  38.                 arr[i] = byte.Parse(Console.ReadLine());
  39.             }
  40.  
  41.             return arr;
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement