title: Counting number of bits in an unsigned long
body: <p>So I've written this function to count the number of bits in a long, which for my purposes includes zeros to the right of the MSB and excludes zeros to its left:</p>
  
  <pre><code>int bitCount(unsigned long bits)
  {
      int len = 64;
      unsigned long mask = 0x8000000000000000;
      while ((bits &amp; mask) == 0 &amp;&amp; len &gt; 0){
          mask &gt;&gt;= 1;
          --len;
      }
      return len;
  }
  </code></pre>
  
  <p>This function works fine for me as far as returning a correct answer, but is there a better (faster or otherwise) way to go about doing this?</p>