title: Swapping individual bits with XOR
body: <p>I was reading  <a href="http://graphics.stanford.edu/~seander/bithacks.html#SwappingBitsXOR" rel="nofollow">this article</a> swapping of individual bits with Xor to  swap the bits of a given number.<br><br>
  As an example of *<em>swapping ranges of bits suppose we have have b = 00101111 (expressed in binary) and we want to swap the n = 3 consecutive bits starting at i = 1 (the second bit from the right) with the 3 consecutive bits starting at j = 5; the result would be r = 11100011 (binary).</em>*but i could not able to understand how it is working.<br>
  Given code is</p>
  
  <pre><code>unsigned int i, j; // positions of bit sequences to swap
  unsigned int n;    // number of consecutive bits in each sequence
  unsigned int b;    // bits to swap reside in b
  unsigned int r;    // bit-swapped result goes here
  
  unsigned int x = ((b &gt;&gt; i) ^ (b &gt;&gt; j)) &amp; ((1U &lt;&lt; n) - 1); // XOR temporary
  r = b ^ ((x &lt;&lt; i) | (x &lt;&lt; j));
  </code></pre>
  
  <p>Please any one clear me how this code is working.</p>