Advertisement
mustafov

Bit Killer

Sep 3rd, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1.  //Bit Killer
  2. //You are given a sequence of bytes. Consider each byte as sequence of exactly 8 bits.  You are given also a number step. Write a //program to remove (kill) all the bits at positions: 3, 3 + step, 3 + 2*step, ... Print the output as a sequence of bytes. In //case the last byte have less than 8 bits, add trailing zeroes at its right end. Bits in each byte are counted from the leftmost //to the rightmost. Bits are numbered starting from 0.
  3.  
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace BitKiller
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             byte n = byte.Parse(Console.ReadLine());
  18.             byte steps = byte.Parse(Console.ReadLine());
  19.             StringBuilder bin = new StringBuilder();
  20.             for (int i = 0; i < n * 8; i += 8)
  21.             {
  22.                 byte number = byte.Parse(Console.ReadLine());
  23.                 string binary = Convert.ToString(number, 2).PadLeft(8, '0');
  24.                 bin = bin.Insert(i, binary);
  25.             }
  26.             StringBuilder newStr = new StringBuilder();
  27.             Console.WriteLine(bin);
  28.             int lenght = bin.Length;
  29.             int index = 0;
  30.             for (int j = 0; j < lenght; j++ )
  31.             {
  32.                 if (j == 1)
  33.                 {
  34.                    //bin = bin.Remove(j, 1);
  35.                 }
  36.                 else if ((j+1) % steps == 0 && j != 0)
  37.                 {
  38.                    //bin = bin.Remove(steps, 1);
  39.                 }
  40.                 else if (bin[j] == '0' && j != 1 &&  (j+1) % steps == 1)
  41.                 {
  42.                     newStr = newStr.Insert(index, '0');
  43.                     index++;
  44.                 }
  45.                 else if (bin[j] == '1' && j != 1 && (j + 1) % steps == 1)
  46.                 {
  47.                     newStr = newStr.Insert(index, '1');
  48.                     index++;
  49.                 }
  50.             }
  51.             string result = newStr.ToString();
  52.             if (result.Length <= 8)
  53.             {
  54.                 result = result.PadRight(8, '0');
  55.                 Console.WriteLine(result);
  56.                 int num = Convert.ToInt32(result,2);
  57.                 Console.WriteLine(num);
  58.             }
  59.             StringBuilder final = new StringBuilder();
  60.             if ( result.Length <= 16 && result.Length > 8)
  61.             {
  62.                 int k = 0;
  63.                 result = result.PadRight(16, '0');
  64.                 for (int p = 0; p < 16; p++)
  65.                 {
  66.                     int t = 0;
  67.                     int ind = 1;
  68.                     for (k = p; k < ind * 8; k++)
  69.                     {
  70.                         final = final.Insert(t, result[k]);
  71.                         t++;
  72.                     }
  73.                     Console.WriteLine(final);
  74.                     string ne = final.ToString();
  75.                     int numb = Convert.ToInt32(ne, 2);
  76.                     Console.WriteLine(numb);
  77.                     p = ind * 7;
  78.                     ind++;
  79.                    
  80.                 }
  81.             }
  82.             if (result.Length <= 24 && result.Length > 16)
  83.             {
  84.                 result = result.PadRight(24,'0');
  85.             }
  86.             if (result.Length <= 32 && result.Length > 24)
  87.             {
  88.                 result = result.PadRight(32, '0');
  89.             }
  90.             Console.WriteLine(result);
  91.                
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement