Pregunta

la línea "si (arg2 y 1)" en C ++ (arg2 es DWORD) es igual a "si (arg2 y 1 == 0)" en C # (arg2 es Uint32), ¿verdad?

Estoy tratando de traducir una función de C ++ a C #, pero me da un error:

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

También me gustaría ser agradecidos si usted podría ver aún más en toda la función para cualquier otro errores.

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

Convertido a C #

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

Función:

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

Esto es lo que aún no estoy seguro:

  1. Si un DWORD en C ++ es UInt32 o Int32 en C #?
  2. si (entero y entero) significa que si (entero y número entero == 0)? // esto es donde se coloca el error he descrito anteriormente.
  3. if (! Entero) significa que si (entero! = 0)?
  4. ¿Por qué operador y no se puede utilizar de forma lógica en C #, lo que significa que requiere un booleano?
  5. "número LARGE_INTEGER = {1, 0}" significa result.lowpart es 1 y result.highpart es 0 o result.Quadpart = 1?

Gracias de antemano!

¿Fue útil?

Solución

Cuando se escribe:

si (arg1 y arg2 == 0)

El compilador entiende:

if (arg1 & (arg2==0))

Usted debe escribir:

if ((arg1 & arg2) == 0)

Esta es la forma en la declaración C ++ debe ser traducido a C #:

if (arg2 & 1) // C++ (arg2 is DWORD)
if ((arg2 & 1) != 0) // C# (arg2 is Uint32)

O, de manera más genérica:

if (Flags & FlagToCheck) // C++
if ((Flags & FlagToCheck) != 0) // C#

En C / C ++, 0 es falso, todo lo demás es cierto.

  1. Usted debe revisar la definición de DWORD, que debería ser (unsigned int), que es UInt32 en C #
  2. si (entero y entero), en C / C ++ significa "si el resultado de la bit a bit y entre los dos números enteros no es 0" (0 es falso, todo lo demás es verdadero).
  3. if (! Entero) significa que si (entero == 0) (de nuevo, 0 es falso, todo lo demás es cierto)
  4. en C #, como en Java pienso, booleanos y los números son dos cosas diferentes, sólo se puede utilizar booleanos en "if", no hay conversión implícita si se utiliza un int: que no se compilará
  5. voy a dejar éste a otra persona, que había necesidad de probar para estar seguro ...

Otros consejos

  1. DWORD es uint32_t en C ++, así UInt32 en C #.
  2. if(a & b) convierte a if((a & b) != 0). != se evalúa antes & por lo tanto la expresión & necesita paréntesis alrededor de ella.
  3. if(x) convierte a if(x != 0)
  4. & es un 'bit a bit y' en C #, como en C ++.
  5. Depende de su estructura C ++.

5 - Significa tanto. Debido LowPart y HighPart son sólo "ventanas" en la memoria del QuadPart, cuando result.LowPart == 1 y Result.HighPart == 0, entonces result.QuadPart será igual a 1.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top