如何让所有排列的bitarray排列、bitarray

2023-09-07 09:13:01 作者:惊慌失措

如何生成所有排列的bitarrayñ大小?

how to generate all permutations for bitarray of n size?

我的意思是,例如,如果1和0阵列整型我可以这样做

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');

和s将包含一些置换,例如101010或100000等。 所以,我可以得到所有排列。 例如,对于N = 3

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

但如何(因为我需要的XOR运算等)做相同的bitarray?

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

推荐答案

我没有VS开放的权利,但你可以使用的 BitArray(字节[])构造。

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);
}

您将不得不尝试并拿出转移逻辑为int转换为字节。

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

如果你需要大于32/64位,那么你显然需要另一种方法。

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