title: Is there any optimization possible for function to reverse the order of bytes in an int32?
body: <p>Someone I know was recently asked this question in a coding interview. They were asked to write a function <code>reverseBytes</code> below such that if you pass it <code>0xabcdef12</code> it'd output <code>0x12efcdab</code>.</p>
  <p>They gave the following solution.</p>
  <pre><code>unsigned int reverseBytes (unsigned int x) {
    unsigned int ans = 0;
    ans = ((x &amp; 0xff000000) &gt;&gt; 24) | ((x &amp; 0x00ff0000) &gt;&gt; 8) |   ((x &amp; 0x000000ff) &lt;&lt; 24) |  ((x &amp; 0x0000ff00) &lt;&lt; 8);   
    return res;
  }
  </code></pre>
  <p>Are there any further ways to optimize it? One can of course not do the 1st and 3rd mask, but they'd still need to do at least 2 bitmask operations, 4 bitwise-shift, and 3 bitwise-or, is that correct?</p>
  <p>According to them, interviewer was expecting further optimizations. I'm at loss to see that there could be further optimization possible. If so, what are they?</p>