title: Fast sign of integer in C
body: <p>There is a sign function in C:</p>
  
  <pre><code>int sign(int x)
  {
      if(x &gt; 0) return 1;
      if(x &lt; 0) return -1;
      return 0;
  }
  </code></pre>
  
  <p>Unfortunately, comparison cost is very high, so I need to modify function in order reduce the number of comparisons.</p>
  
  <p>I tried the following:</p>
  
  <pre><code>int sign(int x)
  {
      int result;
      result = (-1)*(((unsigned int)x)&gt;&gt;31);
  
      if (x &gt; 0) return 1;
  
      return result;
  }
  </code></pre>
  
  <p>In this case I get only one comparison.</p>
  
  <p>Is there any way to avoid comparisons at all?</p>
  
  <p><strong>EDIT</strong> <a href="https://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c">possible duplicate</a> does not give an answer for a question as all answers are C++, uses comparison (that I supposed to avoid) or does not return <code>-1</code>, <code>+1</code>, <code>0</code>.</p>