Hi,

I wish to generate all the binary combinations for an input size n. So, the number of combinations in this case would be (2^n) and if n=2 my output should be :

00
01
10
11

How can i do it? What would be the best way?

Thanks

You mean varible++; , or variable += 1; ?
Perhaps you could go to the extreme of doing it with bit operators(I have for fun), or using the inline assembler to do it. So many options, but which is right for you?

hi,

could you give me an example of both bit operators and inline assembler.

Thanks

Hi,

following is my code.

i wish to store the combinations in a vector as :

000
001
010
011
100
101
110
111

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

int binary(int, vector<int> &);
void print_vec();

int n = 4;	
int comb = pow(2,n)-1;

std::vector<int> ros(comb);
std::vector<vector<int> > col(n);
	
int main() {
	int number;
	cout << comb <<"\n";
	
	for(int k=0; k<=comb; k++)
	{
		number=k;
		if (number < 0) 
			cout << "That is not a positive integer.\n";
		else 
		{
			//cout << number <<" converted to binary is: ";
			int count1=binary(number, ros);
			while(count1<n)
			{
				ros.push_back(0);
				count1++;
			}
			col.push_back(ros);
			ros.clear();
			cout << endl;
		}
	}
	void print_vect();
	return 0;
}

int binary(int number, vector<int> &ros1) 
{
	
	int remainder,count=1;
	if(number <= 1) 
	{
		count = 0;
		ros1.push_back(number);
		//cout << number;
		count++;
	}
	else
	{
		/* There is a right shift operator */
		remainder = number%2;
		count++;
		binary(number >> 1, ros1);    
		//cout << remainder;
		ros1.push_back(remainder);
	}
	return count;
}

void print_vec()
{
	for(int it=0; it<col.size(); it++)
	{
		for(int itt=0; itt<col[it].size(); itt)
		{
			fprintf(stderr,"Here = %d",col[it][itt]);
		}
	}	
}

The problem is my vector is empty when i print it. I am not ale to figure out what is wrong

Thanks

Sounds like a subset generating problem to me.

I did almost the same thing a while ago, try reading this.

Hope that helps.

Why are you making this so complex???

vector<char> container;
    
    cout << "i = 0; i++ till 255" << endl;
    
    for(int i = 0; i < 256; i++)
    {
        container.push_back(i);
        cout << '.';
    }
    
    cout << endl << "Printing: ";
    
    for(int i = 0; i < container.size(); i++)
    {
        cout << bitset<numeric_limits<char>::digits>(container[i]) << endl;
    }

Are you pushing ints into a vector, or strings? If your pushing 010 into a vector of ints your going to have issues. You're going to need to use stringstream to push numbers into a string then push those onto a vector. The binary numbers themselves can be generated with really simple for loop constructs, as forementioned.

Hi,

i am pushing one integer value at a time...So, i am pushing 0, then 1 and so on. Also, i tried the bitset code but the maximum conversion it can do is of integer 255. I think it can convert an integer to a maximum of 8 bits.

Let me know if my understanding is right.

This article has been dead for over six months. Start a new discussion instead.