Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <code>BitConverter.GetBytes(UInt32)</code> to get your <code>byte[]</code>, then call <code>Array.Reverse</code> on the array, then use <code>BitConverter.ToUInt32(byte[])</code> to get your int back out.</p> <p><strong>Edit</strong></p> <p>Here's a more efficient and cryptic way to do it:</p> <pre><code>public static UInt32 ReverseBytes(UInt32 value) { return (value &amp; 0x000000FFU) &lt;&lt; 24 | (value &amp; 0x0000FF00U) &lt;&lt; 8 | (value &amp; 0x00FF0000U) &gt;&gt; 8 | (value &amp; 0xFF000000U) &gt;&gt; 24; } </code></pre> <p>This is what you need to know to understand what's happening here:</p> <ol> <li>A UInt32 is 4 bytes. </li> <li>In hex, two characters represent one byte. 179 in decimal == B3 in hex == 10110011 in binary.</li> <li>A bitwise and (<code>&amp;</code>) preserves the bits that are set in <strong>both</strong> inputs: <code>1011 0011 &amp; 1111 0000 = 1011 0000</code>; in hex: <code>B3 &amp; F0 = B0</code>. </li> <li>A bitwise or (<code>|</code>) preserves the bits that are set in <strong>either</strong> input: <code>1111 0000 | 0000 1111 = 1111 1111</code>; in hex, <code>F0 | 0F = FF</code>.</li> <li>The bitwise shift operators (<code>&lt;&lt;</code> and <code>&gt;&gt;</code>) move the bits left or right in a value. So <code>0011 1100 &lt;&lt; 2 = 1111 0000</code>, and <code>1100 0011 &lt;&lt; 4 = 0011 0000</code>.</li> </ol> <p>So <code>value &amp; 0x000000FFU</code> returns a UInt32 with all but the 4th byte set to 0. Then <code>&lt;&lt; 24</code> moves that 4th byte to the left 24 places, making it the 1st byte. Then <code>(value &amp; 0x0000FF00U) &lt;&lt; 8</code> zeros out everything but the 3rd byte and shifts it left so it is the second byte. And so on. The four <code>(x &amp; y) &lt;&lt; z</code> create four UInt32s where each of the bytes have been moved to the place they will be in the reversed value. Finally, the <code>|</code> combines those UIntt32s bytes back together into a single value.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload