Come utilizzare l'operatore & in C #? È la traduzione del codice corretto?
-
22-08-2019 - |
Domanda
la linea "if (arg2 & 1)" in C ++ (arg2 è DWORD) è uguale a "if (arg2 & 1 == 0)" in C # (arg2 è Uint32), giusto?
Sto cercando di tradurre una funzione da C ++ a C #, ma ottengo un errore:
Operator '&' cannot be applied to operands of type 'uint' and 'bool'
Sarei grato anche se si poteva vedere ulteriormente l'intera funzione per eventuali altri errori.
C ++
DWORD Func_X_4(DWORD arg1, DWORD arg2, DWORD arg3)
{
LARGE_INTEGER result = {1, 0};
LARGE_INTEGER temp1 = {0};
LARGE_INTEGER temp2 = {0};
LARGE_INTEGER temp3 = {0};
LARGE_INTEGER temp4 = {0};
for(int x = 0; x < 32; ++x)
{
if(arg2 & 1)
{
temp1.LowPart = arg3;
temp1.HighPart = 0;
temp2.QuadPart = temp1.QuadPart * result.QuadPart;
temp3.LowPart = arg1;
temp3.HighPart = 0;
temp4.QuadPart = temp2.QuadPart % temp3.QuadPart;
result.QuadPart = temp4.QuadPart;
}
arg2 >>= 1;
temp1.LowPart = arg3;
temp1.HighPart = 0;
temp1.QuadPart *= temp1.QuadPart;
temp2.LowPart = arg1;
temp2.HighPart = 0;
temp3.QuadPart = temp1.QuadPart % temp2.QuadPart;
arg3 = temp3.LowPart;
if(!arg2)
break;
}
return result.LowPart;
}
Convertito in C #
Struttura LARGE_INTEGER:
[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct LARGE_INTEGER
{
[FieldOffset(0)]
public Int64 QuadPart;
[FieldOffset(0)]
public UInt32 LowPart;
[FieldOffset(4)]
public Int32 HighPart;
}
funzione:
public static UInt32 X4(UInt32 arg1, UInt32 arg2, UInt32 arg3)
{
LARGE_INTEGER result = new LARGE_INTEGER();
result.LowPart = 1;
result.HighPart = 0;
LARGE_INTEGER temp1 = new LARGE_INTEGER();
LARGE_INTEGER temp2 = new LARGE_INTEGER();
LARGE_INTEGER temp3 = new LARGE_INTEGER();
LARGE_INTEGER temp4 = new LARGE_INTEGER();
for (int x = 0; x < 32; ++x)
{
if (arg1 & 1 ==0)
{
temp1.LowPart = arg3;
temp1.HighPart = 0;
temp2.QuadPart = temp1.QuadPart * result.QuadPart;
temp3.LowPart = arg1;
temp3.HighPart = 0;
temp4.QuadPart = temp2.QuadPart % temp3.QuadPart;
result.QuadPart = temp4.QuadPart;
}
arg2 >>= 1;
temp1.LowPart = arg3;
temp1.HighPart = 0;
temp1.QuadPart *= temp1.QuadPart;
temp2.LowPart = arg1;
temp2.HighPart = 0;
temp3.QuadPart = temp1.QuadPart % temp2.QuadPart;
arg3 = temp3.LowPart;
if (arg2==0)
break;
}
return result.LowPart;
}
Questo è quello che io non sono ancora sicuro:
- Se un DWORD in C ++ è UInt32 o Int32 in C #?
- se (integer e integer) significa che se (integer e integer == 0)? // questo è dove è collocato l'errore ho descritto sopra.
- if (! Intero) significa che se (intero! = 0)?
- Perché operatore e non può essere usato logicamente in C #, il che significa che richiede un valore booleano?
- "risultato LARGE_INTEGER = {1, 0}" indica result.lowpart è 1 e result.highpart è 0 o result.Quadpart = 1?
Grazie in anticipo!
Soluzione
Dove si scrive:
if (arg1 e arg2 == 0)
Il compilatore comprende:
if (arg1 & (arg2==0))
Si dovrebbe scrivere:
if ((arg1 & arg2) == 0)
Questo è il modo in cui il C ++ dichiarazione deve essere tradotto in C #:
if (arg2 & 1) // C++ (arg2 is DWORD)
if ((arg2 & 1) != 0) // C# (arg2 is Uint32)
In alternativa, in un modo più generico:
if (Flags & FlagToCheck) // C++
if ((Flags & FlagToCheck) != 0) // C#
In C / C ++, 0 è falso, tutto il resto è vero.
- Si dovrebbe controllare la definizione di DWORD, dovrebbe essere (unsigned int), che è UInt32 in C #
- se (integer e integer), in C / C ++ significa "se il risultato del bit a bit e tra i due numeri interi non è 0" (0 è falso, tutto il resto è vero).
- if (! Intero) significa che se (integer == 0) (di nuovo, 0 è falso, tutto il resto è vero)
- in C #, come in Java credo, booleani e numeri sono due cose diverse, è possibile utilizzare solo booleani in "if", non c'è conversione implicita se si utilizza un int: non si compila
- Lascio questo a qualcun altro, avrei bisogno di test per essere sicuri ...
Altri suggerimenti
-
DWORD
èuint32_t
in C ++, quindiUInt32
in C #. -
if(a & b)
converte inif((a & b) != 0)
.!=
viene valutata prima&
così l'espressione&
bisogno parentesi intorno ad esso. -
if(x)
converte inif(x != 0)
-
&
è un 'bit a bit e' in C #, come in C ++. - Dipende dalla vostra struttura di C ++.
5 - Vuol dire entrambe le cose. Perché LowPart e HighPart sono solo "finestre" in memoria di QuadPart, quando result.LowPart == 1 e Result.HighPart == 0, allora result.QuadPart sarà uguale a 1.