title: What are 0xaa and 0x55 doing?
body: <p>I tried Googling, but couldn't find anything comprehensible @.@ ...
  Could someone please explain in layman's terms what is happening in this code?</p>
  
  <p>It's a problem from the book "Cracking the Coding Interview".</p>
  
  <p>"Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g., bit 0 and bit 1 are swapped, bit 2 and 3 are swapped, and so on)."</p>
  
  <p>The way I did it didn't involve bit manipulation because I couldn't figure out how %\ ...</p>
  
  <pre><code>def swap(n):
  
      b = bin(n)[2:]
      print(b)
      if len(b)%2 != 0:
          c = True
          b = b[0] + b
  
      pairs = wrap(b, 2)
      pairs = [i[::-1] for i in pairs]
      ans = ''.join(pairs)
  
      if c: ans = ans[1:]
      print(ans)
  </code></pre>
  
  <p>But now I'm looking at their answer and I don't really get it... (doesn't help that it's not in Python) :</p>
  
  <pre><code>int swapOddEvenBits(int x) {
    return ( ((x &amp; 0xaaaaaaaa) &gt;&gt;&gt; 1) | ((x &amp; 0x55555555) &lt;&lt; 1) );
  </code></pre>