Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- class SignedShort
- {
- static List<short> DecToBin(short num)
- {
- List<short> bin = new List<short>();
- while (num != 0)
- {
- bin.Add((short)(num % 2));
- num = (short)(num / 2);
- }
- bin.Reverse();
- return bin;
- }
- static string endingNums(string strNum, int endingNum)
- {
- StringBuilder newNum = new StringBuilder();
- while (newNum.Length != 16 - strNum.Length)
- {
- newNum.Append(endingNum);
- }
- newNum.Append(strNum);
- return newNum.ToString();
- }
- static void Main()
- {
- short number = short.Parse(Console.ReadLine());
- StringBuilder endNum = new StringBuilder();
- if (number >= 0)
- {
- List<short> NumInBin = DecToBin(number);
- foreach (var item in NumInBin)
- {
- endNum.Append(item);
- }
- Console.WriteLine(endingNums(endNum.ToString(), 0));
- }
- else
- {
- number = (short)(Math.Abs(number) - 1);
- List<short> NumInBin = DecToBin(number);
- for (int i = 0; i < NumInBin.Count; i++)
- {
- if (NumInBin[i] == 0)
- {
- endNum.Append(1);
- }
- else
- {
- endNum.Append(0);
- }
- }
- Console.WriteLine(endingNums(endNum.ToString(), 1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement