Comment utiliser le & opérateur en C#? Est la traduction du code correct?

la ligne "if(arg2 & 1)" en C++(arg2 est DWORD) est égal à "si(arg2 & 1==0)" en C#(arg2 est Uint32),droit?

Je suis en train de traduire une fonction de C++ pour le C#,mais j'obtiens une erreur:

Operator '&' cannot be applied to operands of type 'uint' and 'bool'

Je serais également reconnaissante si vous pouviez voir dans l'ensemble de la fonction pour toutes les autres erreurs.

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;
}

Converti en C#

LARGE_INTEGER structure:

[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct LARGE_INTEGER
{
    [FieldOffset(0)]
    public Int64 QuadPart;
    [FieldOffset(0)]
    public UInt32 LowPart;
    [FieldOffset(4)]
    public Int32 HighPart;
}

Fonction:

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;
    }

C'est ce que je ne suis pas encore sûr:

  1. Si une valeur DWORD en C++ est UInt32 Int32 ou en C#?
  2. if(integer & integer) signifie que si(entier et entier ==0)? //c'est là l'erreur que j'ai décrit ci-dessus est placé.
  3. if(!entier) signifie que si(entier != 0)?
  4. Pourquoi l'opérateur & ne peut pas être utilisé logiquement en C# ,le sens qu'il nécessite un booléen?
  5. "LARGE_INTEGER résultat = {1, 0}" signifie résultat.lowpart est de 1 et le résultat.highpart est de 0 ou de résultat.Quadpart = 1?

Merci d'avance!

OriginalL'auteur Ivan Prodanov | 2009-03-26