Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _08ShortBinary
- {
- class ShortBinary
- {
- static void Main(string[] args)
- {
- Console.Write("Number to convert (from -32,768 to 32,767): ");
- short number = short.Parse(Console.ReadLine());
- List<int> binary = new List<int>();
- int sign = number;
- int count = 0;
- while (number != 0)
- {
- binary.Add(Math.Abs(number % 2));
- number /= 2;
- count++;
- }
- if (sign >= 0)
- {
- while (count < 16)
- {
- binary.Add(0);
- count++;
- }
- }
- else
- {
- while (count < 16)
- {
- binary.Add(1);
- count++;
- }
- }
- binary.Reverse();
- int space = 0;
- foreach (var item in binary)
- {
- Console.Write(item);
- space++;
- if (space % 4 == 0)
- Console.Write(" ");
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment