title: reversing byte order in c
body: <p>I got problem solving 'reverseBytes' in cs:app datalab.</p>
  
  <p>i have to make code that returns reversed byte order.</p>
  
  <p><em>example : input=0x123456, returns=0x563412</em></p>
  
  <p>when i used my code, it can't take a score..</p>
  
  <pre><code>int reverseBytes(int x) {
    int mask=0xff;
    int byte1=x&gt;&gt;24;
    int byte2=(x&gt;&gt;16)&amp;mask;
    int byte3=(x&gt;&gt;8)&amp;mask;
    int byte4=x&amp;mask;
    int result=(byte4&lt;&lt;24)|(byte3&lt;&lt;16)|(byte2&lt;&lt;8)|(byte1);
  
    return result;
  }
  </code></pre>
  
  <p>but, when i used other people's code, it takes a score.</p>
  
  <pre><code>int reverseBytes(int x) {
    int t2=~(0xff&lt;&lt;24);
    int s1=(0xff&lt;&lt;16)+0xff;
    int s2=0xff&lt;&lt;8;
    int s3=(s2&lt;&lt;16)+s2;
    int temp=(x&amp;s1)&lt;&lt;8|((x&amp;s3)&gt;&gt;8&amp;t2);
    int q1=(0xff&lt;&lt;8)+0xff;
    int q2=q1&lt;&lt;16;
    int temp2=(temp&amp;q1)&lt;&lt;16|((temp&amp;q2)&gt;&gt;16&amp;(~q2));
  
    return temp2;
  }
  </code></pre>
  
  <p>i don't know why my code can't works..
  i tested my code and other people's code.
  but i can't find difference between my code's result and another code's result.
  please help me..</p>