简体   繁体   English

使用C#计算二进制数中连续的数

[英]calculating number of consecutive ones in a binary number using c#

I want for the program to iterate through every possible binary number from 00000000 to 11111111 and I it to calculate the number of consecutive "runs" of ones. 我想让程序遍历从00000000到11111111的每个可能的二进制数,并计算出连续的“运行次数”。 Ex) 00000001 and 11100000 both count as a single runs of ones 00001010 and 11101110 both count as two runs of ones 例如)00000001和11100000都计为一次的运行00001010和11101110都计为两次的运行

The problem is, is that it ignores the AND mask part and I don't know why. 问题是,它忽略了AND遮罩部分,我不知道为什么。

{
    static void Main(string[] args)
    {
        //Start
        int stuff = BitRunner8();

        //Display
        Console.Write(stuff);
        Console.ReadKey();
    }
    public static int BitRunner8()
    {
        int uniRunOrNot = 0;
        int uniRunCount = 0;
        int uniRunTotal = 0;

        //iterating from numbers 0 to 255
        for (int x = 0; x < 255; x++)
        {
            //I use 128 as my AND mask because 128 is 10000000 in binary
            for ( int uniMask = 128; uniMask != 0; uniMask >>= 1)
            {

                //This is the if statement that doesn't return true ever
                if ((x & uniMask) != 0)

                {
                    //If the and mask is true, and ther were no previous ones before it, add to the the uniRunCount
                    if (uniRunOrNot == 0)
                    {
                        //Total count of the runs
                        uniRunCount++;
                    }
                    // Making it so that if two consective ones are in a row, the 'if' statement right above would return false,
                    //so that it wouldn't add to the uniRunCount
                    uniRunOrNot++;
                }
                else
                {
                    //add the total number of runs to uniRunTotal, and then reset both uniRunOrNot, and uniRunCount
                    uniRunTotal += uniRunCount;
                    uniRunOrNot = uniRunCount = 0;
                }
            }
        }
        //Divide the final amount by 256 total numbers
        uniRunTotal /= 256;
        return uniRunCount;
    }
}

The problem is that your code ignores runs that include the least significant bit. 问题是您的代码将忽略包含最低有效位的运行。 Your code updates uniRunTotal only when it discovers a zero bit. 您的代码仅在发现零位时更新uniRunTotal When the least significant bit is non-zero, uniRunCount is never added to the total. 当最低有效位不为零时, uniRunCount永远不会添加到总数中。

Add code after the loop to add uniRunCount to fix this problem. 在循环之后添加代码以添加uniRunCount来解决此问题。

You can also fix this issue by applying a sentinel strategy: count bits from the other end, and use nine bits instead of eight, because bit number nine is always zero: 您还可以通过应用哨兵策略来解决此问题:计数另一端的位数,并使用9位而不是8位,因为位数9始终为零:

for (int uniMask = 1; uniMask <= 256; uniMask <<= 1)

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

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