Note that there are some explanatory texts on larger screens.

plurals
  1. POExtracting numbers from a 32-bit integer
    primarykey
    data
    text
    <p>I'm trying to solve a riddle in a programming test. </p> <p>Disclaimer: It's a test for a job, but I'm not looking for an answer. I'm just looking for an understanding of how to do this. The test requires that I come up with a set of solutions to a set of problems within 2 weeks, and it doesn't state a requirement that I arrive at the solutions in isolation.</p> <p>So, the problem:</p> <p>I have a 32-bit number with the bits arranged like this:</p> <pre><code>siiiiiii iiiiiiii ifffffff ffffffff </code></pre> <p>Where:</p> <ul> <li>s is the sign bit (1 == negative) </li> <li>i is 16 integer bits </li> <li>f is 15 fraction bits</li> </ul> <p>The assignment is to write something that decodes a 32-bit integer into a floating-point number. Given the following inputs, it should produce the following outputs:</p> <pre><code>input output 0x00008000 1.0 0x80008000 -1.0 0x00010000 2.0 0x80014000 -2.5 0x000191eb 3.14 0x00327eb8 100.99 </code></pre> <p>I'm having no trouble getting the sign bit or the integer part of the number. I get the sign bit like this:</p> <pre><code>boolean signed = ((value &amp; (1 &lt;&lt; 31)) != 0); </code></pre> <p>I get the integer and fraction parts like this:</p> <pre><code>int wholePart = ((value &amp; 0x0FFFFFFF) &gt;&gt; 15); int fractionPart = ((value &amp; 0x0000FFFF &gt;&gt; 1)); </code></pre> <p>The part I'm having an issue with is getting the number in the last 15 bits to match the expected values. Instead of 3.14, I get 3.4587, etc.</p> <p>If someone could give me a hint about what I'm doing wrong, I'd appreciate it. More than anything else, the fact that I haven't figured this out after hours of messing with it is kind of driving me nuts. :-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload