Advertisement
Ludmil

C# once and zeros

Apr 6th, 2014
116
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.Text;
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         long number = 4095;//65535;// int.Parse(Console.ReadLine());
  9.         //Console.WriteLine(GetIntBinaryString (number));
  10.         string bin = GetIntBinaryString(number).ToString();
  11.         char[] numBin = new char[16];
  12.         numBin= bin.ToCharArray();//Convert.ToString(number, 2).PadLeft(16, '0').ToCharArray();//
  13.         string[] zeroes = new string[]{
  14.             "###",
  15.             "#.#",
  16.             "#.#",
  17.             "#.#",
  18.             "###"};
  19.         string[] once = new string[] {
  20.             ".#.",
  21.             "##.",
  22.             ".#.",
  23.             ".#.",
  24.             "###" };
  25.         int lineCounter = 0;
  26.         StringBuilder line = new StringBuilder();
  27.         while (lineCounter < zeroes.Length)
  28.         {
  29.             for (int i = 0; i < numBin.Length; i++)
  30.             {
  31.                 if (numBin[i] == '1')
  32.                 {
  33.                     line.Append(once[lineCounter]);
  34.                     if (i < numBin.Length - 1)
  35.                     {
  36.                         line.Append(".");
  37.                     }
  38.                 }
  39.                 else if (numBin[i] == '0')
  40.                 {
  41.                     line.Append(zeroes[lineCounter]);
  42.                     if (i < numBin.Length - 1)
  43.                     {
  44.                         line.Append(".");
  45.                     }
  46.                 }
  47.             }
  48.             Console.WriteLine(line);
  49.             line.Clear();
  50.             lineCounter++;
  51.         }
  52.     }
  53.     static string GetIntBinaryString(long n)
  54.     {
  55.         char[] b = new char[16];
  56.         int pos = 15;
  57.         int i = 0;
  58.  
  59.         while (i < 16)
  60.         {
  61.             if ((n & (1 << i)) != 0)
  62.             {
  63.                 b[pos] = '1';
  64.             }
  65.             else
  66.             {
  67.                 b[pos] = '0';
  68.             }
  69.             pos--;
  70.             i++;
  71.         }
  72.         return new string(b);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement