示例#1
0
        public static int Flip(int num)
        {
            //  11011101111
            bool bitFlippped     = false;
            int  start           = 0;
            int  end             = start;
            int  longestLength   = 0;
            int  lastOffBitIndex = 0;

            for (int i = 0; i < 32; i++)
            {
                bool curBit = BitDriver.Getbit(num, i);
                if (curBit)
                {
                    end++;
                }
                else
                {
                    if (!bitFlippped)
                    {
                        bitFlippped     = true;
                        lastOffBitIndex = i;
                        end++;
                    }
                    else
                    {
                        int length = end - start;
                        if (length > longestLength)
                        {
                            longestLength = length;
                        }

                        i           = lastOffBitIndex + 1;
                        start       = i;
                        end         = start;
                        bitFlippped = false;
                    }
                }
            }

            return(longestLength);
        }
示例#2
0
        private static int FlipPairwise(int num)
        {
            int a     = num;
            int index = 0;

            while (a != 0)
            {
                bool x = (a & 1) == 1;
                bool y = (a & 2) == 2;

                if (x != y)
                {
                    num = BitDriver.Updatebit(num, index, y);
                    num = BitDriver.Updatebit(num, index + 1, x);
                }

                a      = a >> 2;
                index += 2;
            }

            return(num);
        }