繁体   English   中英

如何从位获得所有组合?

[英]How to get all combination from bits?

我正在制作将编写所有可能的子网和主机范围的程序。 例如,我有 4 位用于子网,所以我需要编写所有可能的组合。 Input: 4 : output(array): 0000, 0001, 0010, 0011, 0100,0101...1111 我做的太慢了:增加十进制数 -> 转换成二进制,我想不做转换.

这是我不正确的算法,但它有效

   public List<string> getAllCombination(int bits)
    {
        List<string> strarray = new List<string>();
        string temp = "";
        //make 1st word 
        for(int i = 0;i< bits;i++)
        {
            temp += "0";
        }
        strarray.Add(temp);
        int loops = (int)Math.Pow(2, bits) - 1;
        for(int i = 0; i< loops;i++)
        {
            int smallestBitPosition = -1;
            //find last 1
            for(int j = temp.Length -1 ; j >= 0; j--)
            {
                if (temp[j] == '1')
                    smallestBitPosition = j;

            }
            StringBuilder temp1 = new StringBuilder(temp);
            //if there are no 1 
            if (smallestBitPosition == -1)
            {
                temp1[temp1.Length - 1] = '1';
                temp = temp1.ToString();
                strarray.Add(temp);
                continue;
            }

            int lastZeroPosition = -1;
            //find last zero
            for (int j = smallestBitPosition; j< temp.Length; j++)
            {
                if (temp[j] == '0')
                    lastZeroPosition = j;
            }
            //if theres no 0 
            if(lastZeroPosition == -1 )
            {
                temp1[smallestBitPosition - 1] = '1';
                for(int g = smallestBitPosition ; g  < temp.Length; g++ )
                {
                    temp1[g] = '0';
                }
                temp = temp1.ToString();
                strarray.Add(temp);
                continue;
            }
            //i dont even know how to describe this, without this part it makes for example 101 -> 111, when it should be 110
            else if ((lastZeroPosition + 1 != bits) && temp[lastZeroPosition + 1] == '1')
            {
                temp1[lastZeroPosition] = '1';
                for (int g = lastZeroPosition + 1; g < temp.Length; g++)
                {
                    temp1[g] = '0';
                }
                temp = temp1.ToString();
                strarray.Add(temp);
                continue;
            }
            else
            {
                temp1[lastZeroPosition] = '1';

                temp = temp1.ToString();
                strarray.Add(temp);
                continue;
            }
        }
        return strarray;

您的问题基本上是如何生成具有所有可能的零和一组合的字符深/长序列。 这个问题已经在这里有了答案。

使其适应您的要求很容易: GetNthEnumeration(new[] { "0", "1" }, 3).ToList();

您可以使用一个技巧来非常干净地执行此操作:

假设您希望为数字“位”打开所有位组合

bits = 1111
combination = bits
rv = [bits]
while n != 0:
    combination = (combination - 1) & bits
    rv.append(combination)
return rv

这将为您提供“位”中设置的任何位的每种组合。 这也不需要是 1 位的完整序列。 如果你使用 bits = 1010 它只会返回 1010, 1000, 0010, 0000

如果“位”仅由 1 组成,那么您基本上是通过一次计数 1 从原始数字中减去为零。 (即 & 与 1111 并没有真正做任何事情)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM