How to use the & operator in C #? Is the code translated correctly?

the string "if (arg2 and 1)" in C ++ (arg2 - DWORD) is equal to "if (arg2 and 1 == 0)" in C # (arg2 - Uint32), right?

I am trying to translate a function from C ++ to C #, but I get an error:

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

I would also be grateful if you could see further the whole function for any other errors.

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

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

Functions:

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

This is what I'm not sure yet:

  • Is DWORD in C ++ UInt32 or Int32 in C #?
  • if (integer and integer) means if (integer and integer == 0)? // the error described above is placed here.
  • if (! integer) means if (integer! = 0)?
  • Why can't an operator be used logically in C #, that is, it requires a logical one?
  • "LARGE_INTEGER result = {1, 0}" result.lowpart 1 result.highpart 0 result.Quadpart = 1?

!

+3
3

:

if (arg1 arg2 == 0)

:

if (arg1 & (arg2==0))

:

if ((arg1 & arg2) == 0)

, ++ #:

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

:

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

C/++ 0 , .

  • DWORD, (unsigned int), UInt32 #
  • if (integer integer), C/++ " 0" (0 - false, - true).
  • if (! integer) if (integer == 0) ( , 0 - false, - true)
  • #, Java, , - , if, , int: .
  • - , , ...
+11
  • DWORD uint32_t ++, UInt32 #.
  • if(a & b) if((a & b) != 0). != &, & .
  • if(x) if(x != 0)
  • & "" #, ++.
  • ++.
+4

5 - , . LowPart HighPart "" QuadPart, result.LowPart == 1 Result.HighPart == 0, result.QuadPart 1.

+1
source

Source: https://habr.com/ru/post/1705400/


All Articles