Type in how many bits you want and the program will use recursive calls to generate all possible binary combinations, in order.
Compile and run on a Raspberry Pi, Apple Mac or Linux PC. Alternatively you can paste the code into repl.it
Code below:
// Binary tables - Phil Gardner
#include <iostream> // For screen and keyboard
using namespace std; // Using cout, cin, endl
// - - - - - - - - - -
string pickFrom = "01"; // The different characters that will be used to make the sequences
int numBits; // How many bits to use
unsigned long long combinations; // Counts up how many different sequences have been made so far
// - - - - - - - - - -
void appendToStub( string stub )
{
// Decide whether the sequence of bits is long enough
// If not yet long enough will trigger another recursive call
bool callAgain = ( stub.length()+1 < numBits );
string newSequence;
// First select a 0, then select a 1
for ( int i = 0; i < 2; i++ )
{
newSequence = stub + pickFrom.at( i );
// Display completed sequence of bits
if ( newSequence.length() == numBits )
{
cout << '\t' << newSequence << endl;
combinations++;
} // end of if that decides whether to display
// Decide whether to recursively call again to add new digit to end of this sequence
if ( callAgain )
appendToStub( newSequence );
} // end of for loop
} // end of function
// - - - - - - - - - -
int main()
{
do
{
cout << endl << "How many bits? ";
cin >> numBits;
if ( numBits > 0 )
{
cout << endl;
combinations = 0;
appendToStub( "" );
cout << endl << combinations << " combinations for " << numBits << " bits." << endl;
} // end of if numBits > 0
} while ( true == true );
} // end of main function