1

how to generate all permutations for bitarray of n size?

I mean for example if array of 1 and 0 has integer type I can do like this

for (int i = 0; i <= ~(-1 << n); i++)
   string s = Convert.ToString(i, 2).PadLeft(n, '0');

and s will contain some permutation for example 101010 or 100000 and etc. So I can get all permutations. For example for n=3

000
001
010
011
100
101
110
111

But how to do the same for bitarray?(because I need XOR operations and etc.)

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742

5 Answers5

3

I don't have VS open right now, but you could use the BitArray(byte[]) constructor.

for (var i = 0; i < 1 << n; i++)
{
    byte[] bytes = BitConverter.GetBytes(i);
    var bitArray = new BitArray(bytes);
}

You'll have to experiment and come up with the shifting logic to convert an int into bytes.

If you need greater than 32/64 bits, then you'll obviously need another approach.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • cannot resolve symbol IntToBytes. Or IntToBytes - my own method for converting?and It should return array of bytes? – Артём Царионов Mar 20 '12 at 00:02
  • 1
    `IntToBytes` has been implemented: `BitConverter.GetBytes(int)`. http://msdn.microsoft.com/en-us/library/de8fssa4.aspx – Kendall Frey Mar 20 '12 at 00:06
  • 1
    `BitConverter.GetBytes` is a method which converts an `int` into a `byte[]`. As always be careful about endianness with this method. (What order the `byte`s are in) – Guvante Mar 20 '12 at 00:07
2

Why don't you work with int or long , since you will never able to work with permutations larger than ~2^32(in fact much lesser) in a reasonable time.

for (int i = 0; i < 64; i++)
{
        Console.WriteLine(Convert.ToString(i,2).PadLeft(6,'0'));
}

Output:

000000
000001
000010
000011
000100
000101
000110
000111
001000
001001
001010
001011
001100
etc.
L.B
  • 114,136
  • 19
  • 178
  • 224
1

This is based on a recursive list-filling procedure that I wrote for a recent project.

private void GenerateStringsRecursive(List<string> strings, int n, string cur)
{
    if (cur.Length == n)
    {
        strings.Add(cur);
    }
    else
    {
        GenerateStringsRecursive(strings, n, cur + "0");
        GenerateStringsRecursive(strings, n, cur + "1");
    }
}

Call it like this:

List<string> strings = new List<string>();
GenerateStringsRecursive(strings, n, "");
foreach (string s in strings)
{
    Console.WriteLine(s);
}

I imagine this would be subject to optimizations such as using a StringBuilder etc.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
1

A generalised Permute function that will return all the combinations of any enumerable.

    public static IEnumerable<IEnumerable<T>> Permute<T>(this IEnumerable<T> list)
    {
        for (int i = (1 << list.Count()) - 1; i >= 0; i--)
            yield return list.BitWhere(i);
    }

    public static IEnumerable<T> BitWhere<T>(this IEnumerable<T> list, int selector)
    {
        BitVector32 bits = new BitVector32(selector);
        int c = list.Count();
        for (int i = 1; i <= c; i++)
        {
            if (bits[1 << (c - i)])
                yield return list.ElementAt(i - 1);
        }
    }
Phil Degenhardt
  • 7,215
  • 3
  • 35
  • 46
0

You can use BigInteger for this task. It is essentially a bit array with arithmetic operations, so you can permute the bits by adding one, as you already can do with Int32 and Int64 integers.

At the same time, BigInteger supports bit arithmetics, so you can do xor with ^ operator.

George Polevoy
  • 7,450
  • 3
  • 36
  • 61