Guest User

Untitled

a guest
May 20th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ShowFloat
  7. {
  8.     class ShowFloat
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             float f;
  13.             int exp = 0, mantisse = 1;
  14.             f = float.Parse(Console.ReadLine());
  15.  
  16.             //sign
  17.             if(f<0)
  18.                 Console.WriteLine("sign = 1");
  19.             else
  20.                 Console.WriteLine("sign = 0");
  21.  
  22.             //exp
  23.             if (f >= 1 && f < 2)
  24.                 exp = (1 << 7) - 1;
  25.             else
  26.             {
  27.                 if (f >= 2)
  28.                 {
  29.                     while (f >= 2)
  30.                     {
  31.                         f /= 2;
  32.                         exp++;
  33.                     }
  34.                     exp = (1 << 7) - 1 + exp;
  35.                 }
  36.                 else
  37.                 {
  38.                     while (f < 1)
  39.                     {
  40.                         f *= 2;
  41.                         exp++;
  42.                     }
  43.                     exp = (1 << 7) - 1 - exp;
  44.                 }
  45.             }
  46.             Console.Write("exp = ");
  47.             ShowBin(exp);
  48.  
  49.             //mantisse
  50.             f -= 1;
  51.             float tmp = 0.5F;
  52.             Console.Write("mantisse = ");
  53.             //FIX IT
  54.             if (f == 0.0F)
  55.             {
  56.                 for (int i = 0; i < 23; i++)
  57.                 {
  58.                     Console.Write("0");
  59.                 }
  60.             }
  61.             while (f>0.0F)
  62.             {
  63.                 if (f - tmp >=0)
  64.                 {
  65.                     f -= tmp;
  66.                     Console.Write(1);
  67.                 }
  68.                 else
  69.                     Console.Write(0);
  70.                 tmp /= 2;
  71.             }
  72.             Console.WriteLine();
  73.         }
  74.  
  75.         private static void ShowBin(int exp)
  76.         {
  77.             int br = 8;
  78.             char[] ans = new char[8];
  79.             while (br!=0)
  80.             {
  81.                 br--;
  82.                 ans[br] = (char)((exp % 2) + '0');
  83.                 exp /= 2;
  84.             }
  85.             Console.WriteLine(ans);
  86.         }
  87.     }
  88. }
Add Comment
Please, Sign In to add comment