title: Reverse a 32-bitstring in C
body: <p>Edit: looks like the main issue was with the expected test cases given, rather than my solution. Hopefully, this will be useful to other people that need a quick way to reverse the bit-order of words in C.</p>
  <p>Note: This Q has been downvoted (presumably because people feel that the question wasn't technically correct, as if every question here is going to know the exact issue before asking -- even though, had I known that the supplied expected results are misleading, I wouldn't have needed to ask for help in the first place...).</p>
  <p>I'm having trouble writing a simple C-function to reverse a uint32 bitwise.</p>
  <p>Below is the test code along with the expected results:</p>
  <pre><code>printf(&quot;\nReverse(0xDEADBEEF) = 0x%X\n&quot;, Reverse(0xDEADBEEFU)); // should be 0xf77db57b
  printf(&quot;Reverse(0x12345678) = 0x%X\n&quot;, Reverse(0x12345678U));   // should be 0x3cd4589
  printf(&quot;Reverse(0) = 0x%X\n&quot;, Reverse(0));                      // should be 0
  printf(&quot;Reverse(0xFFFFFFFF) = 0x%X\n&quot;, Reverse(0xFFFFFFFFU));   // should be 0xFFFFFFFFU
  </code></pre>
  <p>But when I run my program, I get the following output:</p>
  <pre><code>Reverse(0xDEADBEEF) = 0xF77DB57B
  Reverse(0x12345678) = 0x1E6A2C48
  Reverse(0) = 0x0
  Reverse(0xFFFFFFFF) = 0xFFFFFFFF
  </code></pre>
  <p>So something is wrong with the 0x12345678U case. Notice that the solution is off by a factor of two (only in that case, the 0xDEADBEEFU and 0 and 0xFFFFFFFF cases are fine).</p>
  <p>Finally, the Reverse function is below:</p>
  <pre><code>uint32_t Reverse(const uint32_t Input)
  {
      uint32_t Out = 0U;
      uint32_t Indx;
      uint32_t N = Input;
      for (Indx = 0U; Indx &lt;= 31U; Indx++)
      {
          Out |= (N &amp; 1U) &lt;&lt; (31U - Indx);
          N &gt;&gt;= 1U;
      }
      return Out;
  };
  </code></pre>
  <p>So I'm stumped on what's causing the issue.</p>
  <p>Any tips or ideas, please?</p>