title: How to see if the two's complement of a number x can be represented in n number of bits
body: <ul>
  <li>fitsBits - return 1 if x can be represented as an </li>
  <li>n-bit, two's complement integer.</li>
  <li>1 &lt;= n &lt;= 32</li>
  <li>Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1</li>
  <li>Legal ops: ! ~ &amp; ^ | + &lt;&lt; >></li>
  </ul>
  
  <p>My code is the following:</p>
  
  <pre><code>int fitsBits(int x, int n) {
      int twos = ~x + 1; //two's complement
      int ans;
      ans = (twos &gt;&gt; (n);
      ans = !ans;
      return ans;
  }
  </code></pre>
  
  <p>Working it on paper, it seems to work correctly but it fails when actually tested and I'm not sure why.</p>