Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- class ShowBinaryCodeOfShort
- {
- static void Main()
- {
- short num = 88;
- Console.WriteLine("\n{0} -> {1}", num, ConvertToBinary(num));
- }
- static string ConvertToBinary(short num)
- {
- StringBuilder result = new StringBuilder() { Length = 16 };
- byte msb = (byte)(num < 0 ? 1 : 0);
- int numAfterMSB = -msb * (2 << 14) + num;
- result[0] = (char)(msb + '0');
- for (int i = 1; i < 16; i++)
- {
- result[16 - i] = (char)(numAfterMSB % 2 + '0');
- numAfterMSB /= 2;
- }
- return result.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement