C# 匹配一个数字中的一系列位,然后将匹配项转换为零?

C# 匹配一个数字中的一系列位,然后将匹配项转换为零?,c#,C#,我的任务是搜索一个数字的二进制表示形式,并替换另一个二进制表示形式的匹配模式。如果我得到匹配,我将第一个整数中的匹配位转换为零,然后继续。 例如,数字469将是111010101,我必须将它与5匹配(101)。这是我到目前为止写的程序。没有按预期工作 using System; namespace Conductors { class Program { static void Main(string[] args) {

我的任务是搜索一个数字的二进制表示形式,并替换另一个二进制表示形式的匹配模式。如果我得到匹配,我将第一个整数中的匹配位转换为零,然后继续。 例如,数字469将是
111010101
,我必须将它与5匹配(
101
)。这是我到目前为止写的程序。没有按预期工作

using System;

namespace Conductors
{
    class Program
    {
        static void Main(string[] args)
        {
            //this is the number I'm searching for a match in
            int binaryTicket = 469;
            //This is the pattern I'm trying to match (101)
            int binaryPerforator = 5;

            string binaryTicket01 = Convert.ToString(binaryTicket, 2);

            bool match = true;
            //in a 32 bit integer, position 29 is the last one I would 
            //search in, since I'm searching for the next 3
            for (int pos = 0; pos < 29; pos++)
            {
                for (int j = 0; j <= 3; j++)
                {
                    var posInBinaryTicket = pos + j;
                    var posInPerforator = j;

                    int bitInBinaryTicket = (binaryTicket & (1 << posInBinaryTicket)) >> posInBinaryTicket;
                    int bitInPerforator = (binaryPerforator & (1 << posInPerforator)) >> posInPerforator;

                    if (bitInBinaryTicket != bitInPerforator)
                    {
                        match = false;
                        break;
                    }
                    else
                    {
                        //what would be the proper bitwise operator here?
                        bitInBinaryTicket = 0;
                    }
                }

                Console.WriteLine(binaryTicket01);
            }
        }
    }
}
使用系统;
名称空间导体
{
班级计划
{
静态void Main(字符串[]参数)
{
//这是我正在搜索的匹配号码
int二进制票证=469;
//这就是我试图匹配的模式(101)
int双穿孔器=5;
string binaryTicket01=Convert.ToString(binaryTicket,2);
布尔匹配=真;
//在32位整数中,位置29是最后一个
//搜索,因为我正在搜索下一个3
对于(int pos=0;pos<29;pos++)
{
对于(int j=0;j posInBinaryTicket;
int-bitInPerforator=(二进制穿孔器和(1>posInPerforator);
if(bitInBinaryTicket!=bitInPerforator)
{
匹配=假;
打破
}
其他的
{
//这里正确的位运算符是什么?
bitInBinaryTicket=0;
}
}
Console.WriteLine(二进制ticket01);
}
}
}
}
几件事:

  • 使用
    uint
    进行此操作。在处理二进制数时,这会使事情变得非常简单
  • 您实际上并没有设置任何内容—您只是在存储信息,这就是为什么您经常打印相同的数字
  • 您应该循环x次,其中x=二进制字符串的长度(不仅仅是29)。不需要内部循环
  • static void Main(字符串[]args)
    {
    //这是我正在搜索的匹配号码
    uint二进制票证=469;
    //这就是我试图匹配的模式(101)
    uint双穿孔器=5;
    var numBinaryDigits=Math.天花(Math.Log(binaryTicket,2));
    对于(变量i=0;i
    请仔细阅读以上代码-如果您有任何不明白的地方,请给我留言,我可以与您一起浏览。

    几件事:

  • 使用
    uint
    进行此操作。在处理二进制数时,这会使事情变得非常简单
  • 您实际上并没有设置任何内容—您只是在存储信息,这就是为什么您经常打印相同的数字
  • 您应该循环x次,其中x=二进制字符串的长度(不仅仅是29)。不需要内部循环
  • static void Main(字符串[]args)
    {
    //这是我正在搜索的匹配号码
    uint二进制票证=469;
    //这就是我试图匹配的模式(101)
    uint双穿孔器=5;
    var numBinaryDigits=Math.天花(Math.Log(binaryTicket,2));
    对于(变量i=0;istatic void Main(string[] args)
    {
        //this is the number I'm searching for a match in
        uint binaryTicket = 469;
        //This is the pattern I'm trying to match (101)
        uint binaryPerforator = 5;
    
        var numBinaryDigits = Math.Ceiling(Math.Log(binaryTicket, 2));
        for (var i = 0; i < numBinaryDigits; i++)
        {
            var perforatorShifted = binaryPerforator << i;
    
            //We need to mask off the result (otherwise we fail for checking 101 -> 111)
            //The mask will put 1s in each place the perforator is checking.
            var perforDigits = (int)Math.Ceiling(Math.Log(perforatorShifted, 2));
            uint mask = (uint)Math.Pow(2, perforDigits) - 1;
    
            Console.WriteLine("Ticket:\t" + GetBinary(binaryTicket));
            Console.WriteLine("Perfor:\t" + GetBinary(perforatorShifted));
            Console.WriteLine("Mask :\t" + GetBinary(mask));
    
            if ((binaryTicket & mask) == perforatorShifted)
            {
                Console.WriteLine("Match.");
                //Imagine we have the case:
    
                //Ticket:
                //111010101
                //Perforator:
                //000000101
    
                //Is a match. What binary operation can we do to 0-out the final 101?
                //We need to AND it with 
                //111111010
    
                //To get that value, we need to invert the perforatorShifted
                //000000101
                //XOR
                //111111111
                //EQUALS
                //111111010
    
                //Which would yield:
                //111010101
                //AND
                //111110000
                //Equals
                //111010000
    
                var flipped = perforatorShifted ^ ((uint)0xFFFFFFFF);
                binaryTicket = binaryTicket & flipped;
            }
        }
    
        string binaryTicket01 = Convert.ToString(binaryTicket, 2);
        Console.WriteLine(binaryTicket01);
    }
    
    static string GetBinary(uint v)
    {
        return Convert.ToString(v, 2).PadLeft(32, '0');
    }