title: Bitwise operation in C to compare two integers
body: <p>I was recently given a quiz in one of my classes. The question is below:</p>
  
  <blockquote>
    <p>Write a function (called <code>cmp</code>) in C that accepts two integers (<code>x</code> and <code>y</code>)
    and returns: <code>-1</code> if <code>x</code> &lt; <code>y</code>, <code>0</code> if <code>x</code> = <code>y</code>, <code>1</code> if <code>x</code> > <code>y</code>. Write <code>cmp</code> as concise as possible.</p>
  </blockquote>
  
  <p>The most concise function that I could think of was:</p>
  
  <pre><code>int cmp(int x, int y) {
      return ((x &lt; y) ? (-1) : ((x == y) ? (0) : (1)));
  }
  </code></pre>
  
  <p>But I have a feeling there could be a bit manipulation that I could use to do this more concisely. Perhaps a combination of <code>&amp;</code> and <code>^</code> ? This has been bothering me for the last few days and I wanted to know if there in fact <b><i>IS</i></b> a better way to do this?</p>