TodorMitev

Untitled

Jan 9th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _08ShortBinary
  7. {
  8.     class ShortBinary
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.Write("Number to convert (from -32,768 to 32,767): ");
  13.             short number = short.Parse(Console.ReadLine());
  14.             List<int> binary = new List<int>();
  15.             int sign = number;
  16.             int count = 0;
  17.             while (number != 0)
  18.             {
  19.                 binary.Add(Math.Abs(number % 2));
  20.                 number /= 2;
  21.                 count++;
  22.             }
  23.             if (sign >= 0)
  24.             {
  25.                 while (count < 16)
  26.                 {
  27.                     binary.Add(0);
  28.                     count++;
  29.                 }
  30.             }
  31.             else
  32.             {
  33.                 while (count < 16)
  34.                 {
  35.                     binary.Add(1);
  36.                     count++;
  37.                 }
  38.             }
  39.             binary.Reverse();
  40.             int space = 0;
  41.             foreach (var item in binary)
  42.             {
  43.                 Console.Write(item);
  44.                 space++;
  45.                 if (space % 4 == 0)
  46.                     Console.Write(" ");
  47.             }
  48.             Console.WriteLine();
  49.  
  50.  
  51.  
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment