Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- //Write a program that shows the binary representation of given 16-bit signed integer number
- class SignedShortToBinary
- {
- static void Main()
- {
- Console.WriteLine("Enter 16-bit signed integer number: ");
- short convert = short.Parse(Console.ReadLine());
- StringBuilder builder = new StringBuilder();
- string result = "";
- if (convert < 0)
- {
- short temp;
- temp = ((short)(Math.Pow(2, 15) - convert));
- for (int i = 0; i < 14; i++)
- {
- if ((temp & 1) == 1)
- {
- builder.Insert(0, 1);
- }
- else
- {
- builder.Insert(0, 0);
- }
- temp /= 2;
- }
- builder.Insert(0, 1);
- result = builder.ToString();
- }
- else
- {
- while (convert > 0)
- {
- if ((convert & 1) == 1)
- {
- builder.Insert(0, 1);
- }
- else
- {
- builder.Insert(0, 0);
- }
- convert /= 2;
- }
- result = builder.ToString();
- }
- Console.WriteLine("Binary representation: \n{0}",result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement