algorithm - Generate all binary strings of length n with k bits set -


What is the best algorithm for finding all binary strings with a set of beet beets? For example, if n = 4 and k = 3, then ... ...

  0111 1011 1101 1110  

To generate me A good way should be given to any N and any Kashmir, so I like to do it with string.

This method generates all integers with N '1 bits properly.

to

Commuting the next bit permutation of lexychography

Suppose we have a pattern of N bits set to 1 in an integer And we want the next sequence of n 1 bit in a dictionary sense. For example, if N3 is and bit pattern is 00010011 , next pattern will be 00010101 , 00010110 , 00011001 , 00011010 , 00011100 , 00100011 , and so forth. The following is a fast way to calculate the numbering.

  unsigned int v; Current bits of bits are unsigned int; The next sequence of // bits is unsigned int t = v. (V - 1); // sets at least 0 significant bits of TV to 1. // To change the next set 1, change the most important bit, // at least 0, and add the necessary 1 bit W = (T + 1). (((~ T & T) - 1)> (__biltin_city (V) + 1));  

The x86 CPU for __ builtin_ctz (v) GNU C compiler gives the number of interval zero. If you are using Microsoft compilers for x86, then the internal _BitScanForward , they both leave a BSF , but may be available equivalent to other architectures. If not, consider using one of the methods to calculate the constant zero bits mentioned earlier. Here is another version which is slow due to its device operator, but it does not need to have zero sequence in it.

  unsigned int t = (v | (v - 1)) + 1; W = T | ((((T & amp; T) / (V & V))> 1) - 1);  

Thank you for Argentina's Diario Sneidermanis, who provided it on November 28, 2009.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -