CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Help With Finding All Combinations for Array length R

1. Junior Member
Join Date
Apr 2008
Posts
1

## Help With Finding All Combinations for Array length R

Hi,

I am currently working on a program that uses Hamming Codes to encode various incoming arrays of 1's and 0's . However, in order to do this i need to create a Generator matrix which requires that I know all possible combinations for an array of length N (by all combinations i just mean binary 1 and 0)... this is so i can find a basis for set and thus create a Matrix.

Was just wondering... can someone explain to me what the logic is/ how to go about using loops to fill in a 2-D array with all possible combinations of 1's and 0's for any n?

Just for clarification heres what I mean:
say array length is 3:

[1 1 0
1 0 1
0 1 1
1 1 1
1 0 0
0 1 0
0 0 1]

Thanks!

2. Member
Join Date
Dec 2006
Posts
166

3. _uj
Banned
Join Date
Nov 2003
Posts
1,405

## Re: Help With Finding All Combinations for Array length R

Originally Posted by shahs8
Was just wondering... can someone explain to me what the logic is/ how to go about using loops to fill in a 2-D array with all possible combinations of 1's and 0's for any n?
For an array of length R there are 2^R combinations. The bit combinations are the binary numbers between 0 and (2^R)-1, like

0=000
1=001
2=010
3=011
4=100
5=101
6=110
7=111

So in principle if R=3 the numbers 0 to 7 represent all bit combinations. It means that these numbers are a dense representation of your arrays.

4. ## Re: Help With Finding All Combinations for Array length R

Originally Posted by shahs8
Hi,

Was just wondering... can someone explain to me what the logic is/ how to go about using loops to fill in a 2-D array with all possible combinations of 1's and 0's for any n?
The combinaisons of 1 and 0 of length n is simply the binary representations of all numbers from 0 to (2^n)-1. It should be easy to generate the binary representation of those numbers.

JeffB
Last edited by JeffB; April 26th, 2008 at 11:26 AM.

5. Elite Member Power Poster
Join Date
Aug 1999
Location
UK
Posts
10,163

## Re: Help With Finding All Combinations for Array length R

If you don't want to mess around with bit-shifting or masking, you could have a look at Integer.toBinaryString(..) or Long.toBinaryString().

Any programming problem can be solved by adding a level of indirection...
Anon.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•