counting number of ones in binary representation of a number
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/109023/best-algorithm-to-count-the-number-of-set-bits-in-a-32-bit-integer">Best algorithm to count the number of set bits in a 32-bit integer?</a>  </p>
</blockquote>



<p>I want to find out how many 1s are there in binary representation of a number.I have 2 logic .</p>

<ol>
<li><pre><code>  int count =0;
int no = 4;

while(no!=0){
    int d = no%2;
    if(d==1)
        count++;
    no = no/2;
    str = str+ d;
}
</code></pre></li>
<li><p>Now  second logic is to keep on masking number iteratively with 1,2,4,8,32 and check if result is 1,2,4, 8..... Am not geting what should be ending condition  for this loop.</p></li>
</ol>
