Advertisement
hristo_bratanov

Binary representation of given 16-bit signed integer

Jan 19th, 2013
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class BinaryOfSShort
  5. {
  6.     static void Main()
  7.     {
  8.         string number = Console.ReadLine();  // read short number
  9.         bool signNegative = false;           // check if input is < 0
  10.         if (Int16.Parse(number) < 0)
  11.         {
  12.             signNegative = true;
  13.         }
  14.         ShowShortInBinary(signNegative, number);
  15.  
  16.     }
  17.  
  18.     static void ShowShortInBinary(bool sign, string input)
  19.     {
  20.         if (sign == false)                                // if digit is < 0
  21.         {
  22.             PrintArray(ReverseArray(DecimalToBinary(input)));
  23.         }
  24.  
  25.         else                                              // Two's complement
  26.         {
  27.             byte bit = 1;                                
  28.             int number = Math.Abs(Int32.Parse(input));
  29.             input = Convert.ToString(number);
  30.             List<byte> inputArr = ChangeDigits(input);
  31.             for (int i = 0; i < inputArr.Count; i++)
  32.             {
  33.                 if (inputArr[i] + bit == 2)
  34.                 {
  35.                     inputArr[i] = 0;
  36.                     bit = 1;
  37.                 }
  38.                 else if (bit == 1)
  39.                 {
  40.                     inputArr[i] = 1;
  41.                     bit = 0;
  42.                 }
  43.                 if (i == (inputArr.Count - 1) && inputArr[i] == 0)
  44.                 {
  45.                     inputArr[i] = 1;
  46.                     break;
  47.                 }
  48.             }
  49.             PrintArray(ReverseArray(inputArr));
  50.  
  51.         }
  52.  
  53.     }
  54.  
  55.     static List<byte> AddInputToShort(List<byte> input)  // if not make the binary 16 digits
  56.     {
  57.         for (int i = 16 - input.Count - 1; i >= 0; i--)
  58.         {
  59.             input.Add(0);
  60.         }
  61.  
  62.         return input;
  63.  
  64.     }
  65.  
  66.     static List<byte> DecimalToBinary(string num)
  67.     {
  68.         List<byte> arr = new List<byte>();
  69.         int dec = Int32.Parse(num);
  70.         while (dec >= 1)
  71.         {
  72.             arr.Add((byte)(dec % 2));
  73.             dec = (short)(dec / 2);
  74.         }
  75.  
  76.         return arr;
  77.     }
  78.  
  79.     static List<byte> ChangeDigits(string num)   // convert 0 to 1, and 1 to 0
  80.     {
  81.         List<byte> changedDigitsNum = new List<byte>();
  82.         List<byte> numArray = DecimalToBinary(num);
  83.         numArray = AddInputToShort(numArray);
  84.         for (int i = 0; i < numArray.Count; i++)
  85.         {
  86.             if (numArray[i] == 1)
  87.             {
  88.                 changedDigitsNum.Add(0);
  89.             }
  90.  
  91.             else
  92.             {
  93.                 changedDigitsNum.Add(1);
  94.             }
  95.  
  96.         }
  97.  
  98.         return changedDigitsNum;
  99.     }
  100.  
  101.     static List<byte> ReverseArray(List<byte> arr)
  102.     {
  103.         List<byte> reversed = new List<byte>();
  104.         for (int i = 0; i < arr.Count; i++)
  105.         {
  106.             reversed.Add(arr[arr.Count - 1 - i]);
  107.         }
  108.  
  109.         return reversed;
  110.     }
  111.  
  112.     static void PrintArray(List<byte> arr)
  113.     {
  114.         if (arr.Count <= 16)
  115.         {
  116.             for (int i = 16 - arr.Count; i > 0; i--)
  117.             {
  118.                 Console.Write("0");
  119.             }
  120.            
  121.         }
  122.  
  123.         for (int i = 0; i < arr.Count; i++)
  124.         {
  125.             Console.Write(arr[i]);
  126.         }
  127.  
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement