the line "if(arg2 & 1)" in C++(arg2 is DWORD) is equal to "if(arg2 & 1==0)" in C#(arg2 is 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'd be also thankful if you could see further in the whole function for any other mistakes.
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;
}
Function:
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:
- Whether a DWORD in C++ is UInt32 or Int32 in C#?
- if(integer & integer) means if(integer and integer ==0)? //this is where the error i described above is placed.
- if(!integer) means if(integer != 0)?
- Why operator & cannot be used logically in C# ,meaning it requires a boolean?
- "LARGE_INTEGER result = {1, 0}" means result.lowpart is 1 and result.highpart is 0 or result.Quadpart = 1?
Thanks in advance!
DWORD
isuint32_t
in C++, thusUInt32
in C#.if(a & b)
converts toif((a & b) != 0)
.!=
is evaluated before&
thus the&
expression needs parentheses around it.if(x)
converts toif(x != 0)
&
is a 'bitwise and' in C#, like in C++.Where you write :
if (arg1 & arg2==0)
The compiler understands :
You should write :
This is the way the C++ statement should be translated to C# :
Or, in a more generic way:
In C/C++, 0 is false, everything else is true.
5 - It means both. Because LowPart and HighPart are just "windows" into QuadPart's memory, when result.LowPart == 1 and Result.HighPart == 0, then result.QuadPart will be equal to 1.