title: Calculating Hamming Weight in O(1)
body: <p>In binary representation, hamming weight is the number of 1's. I came across web and found an O(1) answer to it:</p>
  
  <pre><code>v = v - ((v&gt;&gt;1) &amp; 0x55555555);
  v = (v &amp; 0x33333333) + ((v&gt;&gt;2) &amp; 0x33333333);
  int count = ((v + (v&gt;&gt;4) &amp; 0xF0F0F0F) * 0x1010101) &gt;&gt; 24;
  </code></pre>
  
  <p>However I don't quite understand the algorithm and cannot find a description of it anywhere. Can someone please explain it a little bit especially the last line (what the heck does *0x1010101 and then >> 24 mean)?</p>