Advertisement
sashomaga

32-bit signed floating-point number in IEEE 754 format

Jan 19th, 2013
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.78 KB | None | 0 0
  1. using System;
  2. //Write a program that shows the internal binary representation of given 32-bit signed floating-point number in IEEE 754 format (the C# type float).
  3. //Example: -27,25  sign = 1, exponent = 10000011, mantissa = 10110100000000000000000.
  4. class IEEE754Format
  5. {
  6.     static void Main()
  7.     {        
  8.         float f = -27.25f;    
  9.          
  10.         int bits = BitConverter.ToInt32(BitConverter.GetBytes(f), 0);
  11.         string binary = Convert.ToString(bits, 2);
  12.         binary = binary.PadLeft(32, '0');
  13.  
  14.         Console.WriteLine("Binary representation: {0}",binary);
  15.         Console.WriteLine("Sign: {0}",binary[0]);
  16.         Console.WriteLine("Exponent: {0}", binary.Substring(1, 8));
  17.         Console.WriteLine("Mantissa: {0}", binary.Substring(9));      
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement