2016-01-07 47 views
0

我試圖將java代碼轉換爲c#代碼。我得到這個錯誤操作符>>不能應用於char類型的操作數,並且長

運算符>>不能應用於char和long類型的操作數。

代碼是:

static int getPruningP(byte[] table, long index, long THRESHOLD) 
{ 
    if (index < THRESHOLD) 
    { 
     return tri2bin[table[(int)(index >> 2)] & 0xff] >> ((index & 3) << 1) & 3; 
    } 
    else { 
     return tri2bin[table[(int)(index - THRESHOLD)] & 0xff] >> 8 & 3; 
    } 
} 
+3

'tri2bin'的類型是什麼? – Eran

+0

錯誤消息是自解釋的。你想要做什麼? –

+0

它是'java'還是'c#'?刪除不相關的標籤。 – Codebender

回答

1

您需要做按位與之前的long參數強制轉換爲int。 使用

return tri2bin[table[(int)(index >> 2)] & 0xff] >> (((int)index & 3) << 1) & 3; 

,而不是

return tri2bin[table[(int)(index >> 2)] & 0xff] >> ((index & 3) << 1) & 3; 

Binary & operators are predefined for the integral types and bool and the & operator evaluates both operators regardless of the first one's value.

因此,你需要匹配的類型爲您&運營商,目前你做long & int

+0

謝謝你的工作。 – Ranjith

+0

t與'&'或shift運算符無關 - 可以將'long'和'int'與按位運算符結合使用。你只需要投射返回值。你的解決方案是可行的,因爲'index'上的轉換將返回值更改爲'int',但最直接的解決方案是轉換整個返回值。 –

0

實際上,無關與「&」或移位運算 - 函數返回「廉政」和return語句的結果是「長」,所以你需要轉換的返回值:

static int getPruningP(byte[] table, long index, long THRESHOLD) 
{ 
    if (index < THRESHOLD) 
    { 
     return (int)(tri2bin[table[(int)(index >> 2)] & 0xff] >> ((index & 3) << 1) & 3); 
    } 
    else { 
     return (int)(tri2bin[table[(int)(index - THRESHOLD)] & 0xff] >> 8 & 3); 
    } 
} 
相關問題