/// <summary> /// Returns the index of the first bit that is cleared. /// -1 is returned if all bits are set. /// </summary> /// <returns></returns> public int FindClearedBit() { //parse each item, 32 bits at a time int count = m_array.Length; for (int x = m_lastFoundClearedIndex >> BitsPerElementShift; x < count; x++) { //If the result is not -1 (all bits set), then use this element if (m_array[x] != -1) { int position = BitMath.CountTrailingOnes((uint)m_array[x]) + (x << BitsPerElementShift); m_lastFoundClearedIndex = position; if (m_lastFoundClearedIndex >= m_count) { return(-1); } return(position); } } return(-1); }