title: Counting consecutive 1's in C
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3304705/finding-consecutive-bit-string-of-1-or-0">Finding consecutive bit string of 1 or 0</a>  </p>
  </blockquote>
  
  
  
  <p>Is it possible to count, from left, consecutive 1's in an integer?
  So: total number of consecutive set bits starting from the top bit.</p>
  
  <p>Using only:</p>
  
  <pre><code>! ~ &amp; ^ | + &lt;&lt; &gt;&gt;
  </code></pre>
  
  <p><code>-1</code>= <code>0xFFFFFFFF</code> would return 32</p>
  
  <p><code>0xFFF0F0F0</code> would return 12 (FFF = 111111111111) </p>
  
  <p>No loops, unfortunately.</p>
  
  <p>Can assume the machine:</p>
  
  <ol>
  <li><p>Uses 2s complement, 32-bit representations of integers.</p></li>
  <li><p>Performs right shifts arithmetically.</p></li>
  <li><p>Has unpredictable behavior when shifting an integer by more
   than the word size.</p></li>
  </ol>
  
  <p>I'm forbidden to:</p>
  
  <ol>
  <li><p>Use any control constructs such as if, do, while, for, switch, etc.</p></li>
  <li><p>Define or use any macros.</p></li>
  <li><p>Define any additional functions in this file.</p></li>
  <li><p>Call any functions.</p></li>
  <li><p>Use any other operations, such as &amp;&amp;, ||, -, or ?:</p></li>
  <li><p>Use any form of casting.</p></li>
  <li><p>Use any data type other than int.  This implies that you
   cannot use arrays, structs, or unions.</p></li>
  </ol>
  
  <p>I've looked at 
  <a href="https://stackoverflow.com/questions/3304705/finding-consecutive-bit-string-of-1-or-0">Finding consecutive bit string of 1 or 0</a>
  It's using loops, which I can't use. I don't even know where to start.</p>
  
  <p>(Yes, this is an assignment, but I'm simply asking those of you skilled enough for help. I've done pretty much all of those I need to do, but this one just won't work.)</p>
  
  <p>(For those downvoting simply because it's for school:
  FAQ:
  1 a specific programming problem, check
  2 However, if your motivation is “I would like others to explain ______ to me”, then you are probably OK.)</p>