Advertisement
Guest User

BitsUp

a guest
Apr 14th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Exam05
  8. {
  9.     class Exam05
  10.     {
  11.         static void Main()
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             int step = int.Parse(Console.ReadLine());
  15.             int[] numbers = new int[n];
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 numbers[i] = int.Parse(Console.ReadLine());
  19.             }
  20.             int[] bits = new int[8 * n];
  21.             for (int i = 0; i < bits.Length; i++)
  22.             {
  23.                 bits[i] = 0;
  24.             }
  25.             int counter = bits.Length-1;
  26.             for (int i = numbers.Length-1; i >= 0; i--)
  27.             {
  28.                 int copy = numbers[i];
  29.                 int counterCopy = counter;
  30.                 while (copy!=0)
  31.                 {
  32.                     bits[counterCopy] = copy % 2;
  33.                     copy /= 2;
  34.                     counterCopy--;
  35.                 }
  36.                 counter -= 8;
  37.             }
  38.             int curPos = 1;
  39.             while (true)
  40.             {
  41.                 if (curPos>bits.Length-1)
  42.                 {
  43.                     break;
  44.                 }
  45.                 bits[curPos] = 1;
  46.                 curPos += step;
  47.             }
  48.             for (int i = 0; i < bits.Length; i+=8)
  49.             {
  50.                 StringBuilder builder = new StringBuilder();
  51.                 builder.Append(bits[i]);
  52.                 builder.Append(bits[i+1]);
  53.                 builder.Append(bits[i+2]);
  54.                 builder.Append(bits[i+3]);
  55.                 builder.Append(bits[i+4]);
  56.                 builder.Append(bits[i+5]);
  57.                 builder.Append(bits[i+6]);
  58.                 builder.Append(bits[i+7]);
  59.                 string binary = builder.ToString();
  60.                 int number = Convert.ToInt32(binary, 2);
  61.                 Console.WriteLine(number);
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement