title: In C - counting sequences of 1's
body: <p>Given a binary number such as "100001111111110000001" where each bit is placed in an array so that the array looks like the following:
  [1,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1]</p>
  
  <p>I want to locate the starting index and ending index of any sequence of 1's.</p>
  
  <p>Desired output (reading from right to left):</p>
  
  <p>-starting index = 0</p>
  
  <p>-ending index = 0</p>
  
  <p>(Get this numbers a do something accordingly)</p>
  
  <p>Then:</p>
  
  <p>-starting index = 7</p>
  
  <p>-ending index = 15</p>
  
  <p>(Get this numbers a do something accordingly)</p>
  
  <p>last occurrence -</p>
  
  <p>-starting index = 20</p>
  
  <p>-ending index  = 20</p>
  
  <p>(Get this numbers a do something accordingly)</p>
  
  <p>How can I accomplish the desired output in C?</p>
  
  <p>Attaching code I tried:</p>
  
  <pre><code>   for (int i = 0; i&lt; counter; i++) {
      if (bitArray[i] == 0) {
        while (bitArray[i] == 0) {
          startP++;
          endP++;
          i++;
        }
      }
      if(bitArray[i] == 1) {
          startP++;
          while(bitArray[i+1]==1) {
            endP++;
            i++;
          }
      }
      if (endP == startP) {
          //do something
      } else if (endP == (startP + 1)) {
      //do something
      } else if (endP &gt; (startP + 1)) {
      // do something
      }
      endP++;
      startP = endP;
   }
  </code></pre>
  
  <p>*counter = the len of the array</p>
  
  <p>*endP and startP int, intiliazed.</p>
  
  <p>*bitArray the array as described.</p>