06-14-2011 03:16 PM
Hello,
Using LabVIEW, I need to generate all bit patterns of length N containing at most ones x ones and N-x zeros, for example if N=4, x=1 and the patterns would be 1000, 0100, 0010, 0001.
Thanks in advance.
Solved! Go to Solution.
06-14-2011 03:41 PM
This sounds a lot like a homework assignment.
Do you need the value as a number of a string of ASCII 0s and 1s? This can easily be done using an array. You would also to initialize the array to the specific size. Use a FOR loop to iterate the number of bits you have and use the base array you created with a replace array to replace the specific bit position.
If this is indeed a homework assignment this should give you enough to get a start on it. If it is not an assignment post what you have tried so far and we can go from there.
06-14-2011 05:43 PM
Firstly, thank you for your reply. Secondly, its not a home work problem, there is huge application behind it, think about generating 32 C 6 combinations using binary digits, (huge number). I would be happy if you are able to give proper answer rather than commenting like that.
@Mark Yedinak wrote:
This sounds a lot like a homework assignment.
Do you need the value as a number of a string of ASCII 0s and 1s? This can easily be done using an array. You would also to initialize the array to the specific size. Use a FOR loop to iterate the number of bits you have and use the base array you created with a replace array to replace the specific bit position.
If this is indeed a homework assignment this should give you enough to get a start on it. If it is not an assignment post what you have tried so far and we can go from there.
06-14-2011 05:56 PM
My usual code to generate permutations with repeated elements adapted to this problem. I assume you want arrays of 1's and 0's.
PermuteBits.vi will generate the next combination in the sequence based on the value of the previous iteration. Using this method does not require you carry around all of the combinations.
GetBitPermutations.vi will retrieve all combinations if that is what you desire.
06-14-2011 06:04 PM - edited 06-14-2011 06:04 PM
I apologize if I sounded gruff but i have a policy of not providing solutions to questions that sound like a homework assignment. Your original question sounded very much like that. I also missed the mark on my suggestion in that it would only work for the general case with a single bit set. However, Darin provided a very elegant solution before I could post anything. No need for me to whip something up now.
06-15-2011 03:18 AM
Thank you very much Darin. It is really a great starting point for our application, I appreciate it.
06-15-2011 03:19 AM
Thats ok Mark, No problem, thanks a lot.