// Arup Guha
// 10/14/2010
// Example for COT 4210
// Shows an enumerator constructed from a decider.
// The values enumerated are those that have consecutive 1s in their binary
// representation.

void enumerator(int max);
int consec_ones(int n);

int main() {
    
    enumerator(100);
    
    system("PAUSE");
    return 0;
}

// Enumerator for values whose binary representation contains at least 2 
// consecutive 1s. Enumerates values from 0 to max, inclusive.
void enumerator(int max) {
     
    int i;
    for (i=0; i<=max; i++)
        if (consec_ones(i))
            printf("%d\n", i);
}

// Returns true iff n's binary representation has consecutive ones.
int consec_ones(int n) {
    
    int ones = 0;

    // Split off each binary digit from least to most significant.
    while (n > 0) {
          
        // Got a one.
        if (n%2)
            ones++;
        else
            ones = 0;
            
        // Two ones in a row, we can accept!
        if (ones >= 2)
            return 1;
            
        n /= 2;  // Strip off the last digit.
    }
    
    // Never found 2 1s in a row.
    return 0;
}
