tanijja

14. Decimal to Binary Number

Apr 3rd, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using System;
  2.     class DecimalBinaryNumber
  3.     {
  4.         static void Main()
  5.         {
  6.         long n = 236476736;
  7.         int m = 0;
  8.         int x = 0;
  9.         for (int i = 0; Math.Pow(2, i) < n;i++, x++)
  10.         {
  11.         }
  12.         m = (int)(Math.Pow(2, x) - n);
  13.                
  14.             if (m == 0)
  15.             {
  16.                 long[] bits = new long[x + 1];
  17.                 bits[x] = 1;
  18.                 Console.Write(bits[x]);
  19.                 for (int c = bits.Length-2; c >= 0 ; c--)
  20.                 {
  21.                 bits[c] =0;
  22.                 Console.Write(bits[c]);
  23.                 }
  24.                 Console.WriteLine();
  25.             }
  26.             if (m > 0)
  27.             {
  28.                 long[] bits = new long[x];
  29.                 bits[x - 1] = 1;
  30.                 Console.Write(bits[x - 1]);
  31.                 long m2 = n - (int)Math.Pow(2, x - 1);
  32.                 for (int c = bits.Length - 2; c >= 0; c--)
  33.                 {
  34.                     if (Math.Pow(2, c) <= m2)
  35.                     {
  36.                         bits[c] = 1;
  37.                         Console.Write(bits[c]);
  38.                         m2 -= (int)Math.Pow(2, c);
  39.  
  40.                     }
  41.                     else
  42.                     {
  43.                         bits[c] = 0;
  44.                         Console.Write(bits[c]);
  45.                     }
  46.                        
  47.                 }
  48.                 Console.WriteLine();
  49.             }
  50.         }
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment