Question: Output should be like
0
1
10
11
100
101
110
111
1000
1001
1010
1011
1100
1101
1110
1111
10000
....etc

Here this should be generated without conversion from number. For example we should not generate 111 by converting 7 to binary.

Answer: 
As the only feature you need is to increment, you could readily write a class:

```
 class BinaryNumber {
 public:
      uint8_t data[64];  // This is wasted space, see notes.
      bool increment();
 };
 std::ostream & operator<<(std::ostream &, const BinaryNumber &);

```

It might be that bitset is a better data structure than to waste all that space with 8-bit values when we only care about 1 of the bits, but I wanted to keep this simple.

Incrementing a value isn't bad.

```
// Returns false once we hit max value
bool BinaryNumber::increment() {
    bool retVal = false;
    for (int index = 0; index < 64; ++index) {
        if (data[index] == 0) {
            data[index] = 1;
            retVal = true;
            break;
        }
        else {
            data[index] = 0;
        }
    }
    return retVal;
}

```

Think about incrementing the value 0. Look at the code. If the first bit is a 0, we turn it into a one and break the loop (returning true because we broke early).

If it's a 1, we flip it back to zero and recurse, incrementing at index [1]. Loop until we hit a zero so we don't need to carry any further 1s.

I'll leave the write method to you.

This is not the most efficient method, but it's clean and easy to understand.
Since the goal is to generate a string for output, it makes sense to use `std::string` to hold the current value. Getting the next value just means adding 1 to the current value and handling the carries, just as you’d do by hand. So;

```
class binary_string {
public:
    binary_string() : text(‘0’) {}
    const std::string& get() const { return text; }
    bool next();
private:
    std::string text;
};

bool binary_string::next() {
    std::size_t pos;
    for (pos = 0; pos < text size(); ++pos) {
        if (text[pos] == ‘0’) {
            text[pos) = ‘1’:
            break;
        else
            text[pos] = ‘0’;
    }
    if (pos == 64)
        return false;
    if (pos == text.size())
        text.push_back(‘0’)
    return true;
}

```

To use:

```
int main() {
    binary_string str;
    do {
        std::cout << str.get();
    } while (str.next());
    return 0;
}

```

Be prepared for this to take a **long** time to finish.