Advertisement
ivandrofly

Decimal to Binary

Jun 29th, 2015
1,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 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 Decimal_to_Binary
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var n = 1024;
  14.             DecimalToBinary(8);
  15.         }
  16.  
  17.         static void DecimalToBinary(int n)
  18.         {
  19.             int[] array = new int[(int)Math.Log(n, 2) + 1];
  20.             var idx = array.Length - 1; // no reverse needed ;)
  21.             while (n > 0)
  22.             {
  23.                 var rem = n % 2;
  24.                 array[idx] = rem;
  25.                 n /= 2;
  26.                 idx--;
  27.             }
  28.             //Array.Reverse(array);
  29.             for (int i = 0; i < array.Length; i++)
  30.             {
  31.                 Console.Write(array[i]);
  32.             }
  33.             Console.ReadLine();
  34.         }
  35.     }
  36. }
  37. https://www.youtube.com/watch?v=4vNlt_EDw1Q
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement