title: Format a float using IEEE-754
body: <p>I have an assignment in C where I'm given a float number and have to print it using the IEEE-754 format:</p>
  
  <pre><code>__(sign)mantissa * 2^(exponent)__
  </code></pre>
  
  <p>The sign would be either '-' or ' ', the exponent an int value and the mantissa a float value. I also cannot use<code>struct</code>s or any function present in any library other than the the ones in stdio.h (for the printf function). This should be done using bitwise operations.</p>
  
  <p>I was given two functions:</p>
  
  <ul>
  <li><strong>unsignedToFloat</strong>: given an unsigned int, returns a float with the same bits;</li>
  <li><strong>floatToUnsigned</strong>: given a float, returns an unsigned int with the same bits;</li>
  </ul>
  
  <p>After getting the float representation as an unsigned int, I have managed to determine the sign and exponent of any float number. I have also managed to determine the mantissa in bits. For example, given the float 3.75:</p>
  
  <pre><code>&gt; bit representation: 01000000011100000000000000000000;
  &gt; sign: 0 (' ');
  &gt; exponent = 10000000 (128 - bias = 1)
  &gt; mantissa = 11100000000000000000000
  </code></pre>
  
  <p>Now, in this example, I should represent the mantissa as "1.875". However, I have not been able to do this conversion. So far, I have tried to create another float as 000000000[mantissa], but it gives me the wrong result (which I now understand why). I was instructed that the mantissa has a 1 in the beginning, meaning in this example, the mantissa would become 1.111, but I wasn't instructed on how exactly to do this. I tried searching online, but couldn't find any way of adding a 1 to the beginning of the mantissa.</p>
  
  <p>I also had the idea of doing this portion by going through every bit of the mantissa and getting its decimal representation, and then adding 1. In this example, I would do the following:</p>
  
  <pre><code>&gt; 11100000000000000000000
  &gt; as_float = 2^-1 + 2^-2 + 2^-3
  &gt; as_float += 1
  </code></pre>
  
  <p>However, this approach seems very hack-y, slow and could perhaps give me wrong result.</p>
  
  <p>With this said, I am completely out of ideas. How would I represent the mantissa of a float number as its own thing?</p>