Advertisement
Guest User

Bit Inverter

a guest
May 4th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 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.  
  8. class Program
  9. {
  10.     static List<string> splitEvery8Bits(string bitwize)
  11.     {
  12.         var list = new List<string>();
  13.         int interval = 8;
  14.         int howManyTimes = bitwize.Length / interval;
  15.         int start = 0;
  16.         for (int i = 0; i < howManyTimes; i++)
  17.         {
  18.             list.Add(bitwize.Substring(start, interval));
  19.             start += interval;
  20.         }
  21.         return list;
  22.     }
  23.     static void Main(string[] args)
  24.     {
  25.         byte n = byte.Parse(Console.ReadLine());
  26.         byte step = byte.Parse(Console.ReadLine());
  27.         StringBuilder sbTheGreat = new StringBuilder(n * 8);
  28.  
  29.         for (int i = 0; i < n; i++)
  30.         {
  31.             byte numberTOInsert = byte.Parse(Console.ReadLine());
  32.             string representBits = Convert.ToString(numberTOInsert, 2).PadLeft(8, '0');
  33.             sbTheGreat.Append(representBits);
  34.         }
  35.  
  36.         string temp = sbTheGreat.ToString();
  37.         char[] ableTOChange = temp.ToCharArray();
  38.         int position = 0;
  39.  
  40.         while (position < ableTOChange.Length)
  41.         {
  42.             if (ableTOChange[position] == '1')
  43.             {
  44.                 ableTOChange[position] = '0';
  45.             }
  46.             else if (ableTOChange[position] == '0')
  47.             {
  48.                 ableTOChange[position] = '1';
  49.             }
  50.             position += step;
  51.         }
  52.  
  53.         temp = new string(ableTOChange);
  54.  
  55.         var ourList = splitEvery8Bits(temp);
  56.  
  57.         foreach (var item in ourList)
  58.         {
  59.             byte turnAround = Convert.ToByte(item, 2);
  60.             Console.WriteLine(turnAround);
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement