如何使用&放大器;运营商在C#中?是代码正确的翻译? [英] How to use the & operator in C#? Is the the translation of the code correct?
问题描述
行如果(ARG2&安培; 1)C ++(Arg2为DWORD)等于如果(ARG2&安培; 1 == 0)?在C#(ARG2是UINT32),右
the line "if(arg2 & 1)" in C++(arg2 is DWORD) is equal to "if(arg2 & 1==0)" in C#(arg2 is Uint32),right?
我想转换从C ++到C#功能,但我得到一个错误:
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;
}
转换为C#
Converted to C#
LARGE_INTEGER结构:
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;
}
这是我还没有确定:
- 不管是C ++中的DWORD是UInt32的或的Int32在C#
- 如果(整数安培;整数)?意味着如果(整数和整数== 0)? //这是我上述错误的放置位置。
- 如果(!整数),如果是指(整数!= 0)?
- 为什么运营商和放大器;不能在C#逻辑地使用,这意味着它需要一个布尔?
- LARGE_INTEGER结果= {1,0}指result.lowpart是1和result.highpart是0或result.Quadpart = 1?
- 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?
在此先感谢!
推荐答案
您写的:
如果(ARG1&安培; ARG2 == 0)
if (arg1 & arg2==0)
编译理解:
if (arg1 & (arg2==0))
您应该写:
if ((arg1 & arg2) == 0)
这是C ++的语句应该被翻译成C#的方式:
This is the way the C++ statement should be translated to C# :
if (arg2 & 1) // C++ (arg2 is DWORD)
if ((arg2 & 1) != 0) // C# (arg2 is Uint32)
或者,在一个更通用的方法!
Or, in a more generic way:
if (Flags & FlagToCheck) // C++
if ((Flags & FlagToCheck) != 0) // C#
在C / C ++,0是假的,其他的一切都是真实的。
In C/C++, 0 is false, everything else is true.
- 您应该检查DWORD的定义,它应该是(无符号INT),这是UInt32的在C#
- 如果(整数放大器;整数),在C / C ++的意思是如果按位和两个整数之间的结果不为0(0是假的,一切是如此)。
- 如果(!整数)意味着,如果(整数== 0)(同样,0是假的,其他的一切都是真实的)
- 在C#中,就像在Java中,我认为,布尔和数字是两回事,你只能用在布尔if语句,不存在隐式转换,如果你使用int:它不会编译
- 我将离开这个给别人,我。 ð需要测试,以确保...
- You should check the definition of DWORD, it should be (unsigned int), which is UInt32 in C#
- if (integer & integer), in C/C++ means "if the result of the bitwise and between the two integers is not 0" (0 is false, everything else is true).
- if (!integer) means if (integer == 0) (again, 0 is false, everything else is true)
- in C#, like in Java I think, booleans and numbers are two different things, you can only use booleans in "if" statements, there is not implicit conversion if you use an int : it won't compile.
- I'll leave this one to someone else, I'd need to test to be sure...
这篇关于如何使用&放大器;运营商在C#中?是代码正确的翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!