2016-04-29 26 views
0

我的任務是搜索數字的二進制表示並替換數字的另一個二進制表示的匹配模式。如果我得到一個匹配,我將第一個整數的匹配位轉換爲零,然後繼續。 例如,數字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); 
      } 
     } 
    } 
} 
+0

我會建議這張發佈@ http://codereview.stackexchange.com/而不是SO。完整的代碼無法正常工作,因爲您期望在此處更好地進行審閱。 – Mikanikal

+0

@Mikanikal CodeReview適用於*完成*的工作的完整代碼,但需要反饋以獲得更好/更清潔的解決方案。如果OP在codereview上發佈它,它將被關閉。 SO是這個問題的正確網站。然而,目前還不清楚「不按預期工作」可能意味着一百萬件不同的事情。 – Rob

+0

@Rob這是我的意圖,如果它沒有聽起來(讀)。正在工作的代碼,但並不如預期,這意味着他需要更好的解決方案。因此,從OP中更清晰的問題,我想我會收回我的評論。 – Mikanikal

回答

2

幾件事情:

  1. 使用uint這一點。在處理二進制數字時使事情變得更加簡單。
  2. 你並沒有真正設置任何東西 - 你只是存儲信息,這就是爲什麼你經常打印出相同的數字。
  3. 你應該循環x次,其中x =二進制字符串的長度(不僅僅是29)。有沒有必要爲內環

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

請仔細閱讀了上面的代碼 - 如果有什麼你不明白,給我留下了評論,我可以通過它陪你一起跑。

+0

解決方案有效。非常感謝!我還沒有把頭圍住。我可能會在某個時候利用你的報價。對此,我真的非常感激。 –

+0

@TsvetanDimoff無後顧之憂。我已經用一些調試代碼更新了它,所以希望你能看到轉換的工作方式和我們正在使用的二進制數 – Rob

+0

@Rob,我想你的意思是'uint'而不是'unit'。我想知道你在說什麼,哈哈。 – Andrew