Binary combinations

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 102
Reputation: guest7 is an unknown quantity at this point 
Solved Threads: 0
guest7 guest7 is offline Offline
Junior Poster

Binary combinations

 
0
  #1
Jan 8th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Binary combinations

 
0
  #2
Jan 8th, 2009
Chris

edit: just add one to the number lol and loop through??
Last edited by Freaky_Chris; Jan 8th, 2009 at 4:44 pm.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 949
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Binary combinations

 
0
  #3
Jan 8th, 2009
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?
Last edited by MosaicFuneral; Jan 8th, 2009 at 5:19 pm.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 102
Reputation: guest7 is an unknown quantity at this point 
Solved Threads: 0
guest7 guest7 is offline Offline
Junior Poster

Re: Binary combinations

 
0
  #4
Jan 8th, 2009
hi,

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

Thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 102
Reputation: guest7 is an unknown quantity at this point 
Solved Threads: 0
guest7 guest7 is offline Offline
Junior Poster

Re: Binary combinations

 
0
  #5
Jan 8th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 949
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Binary combinations

 
0
  #6
Jan 8th, 2009
Look up "C++ bit operations"

Inline assembler is platform and compiler specific.
http://www.penguin.cz/~literakl/intel/intel.html
http://www.ibiblio.org/gferg/ldp/GCC...bly-HOWTO.html
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 102
Reputation: guest7 is an unknown quantity at this point 
Solved Threads: 0
guest7 guest7 is offline Offline
Junior Poster

Re: Binary combinations

 
0
  #7
Jan 8th, 2009
if would be great if u can post an example
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Binary combinations

 
0
  #8
Jan 8th, 2009
Sounds like a subset generating problem to me.

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

Hope that helps.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 949
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Binary combinations

 
0
  #9
Jan 8th, 2009
Why are you making this so complex???

  1. vector<char> container;
  2.  
  3. cout << "i = 0; i++ till 255" << endl;
  4.  
  5. for(int i = 0; i < 256; i++)
  6. {
  7. container.push_back(i);
  8. cout << '.';
  9. }
  10.  
  11. cout << endl << "Printing: ";
  12.  
  13. for(int i = 0; i < container.size(); i++)
  14. {
  15. cout << bitset<numeric_limits<char>::digits>(container[i]) << endl;
  16. }
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Binary combinations

 
0
  #10
Jan 9th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC