繁体   English   中英

如何在C#中使用&运算符? 代码的翻译是否正确?

[英]How to use the & operator in C#? Is the the translation of the code correct?

C ++中的“if(arg2&1)”行(arg2是DWORD)等于C#中的“if(arg2&1 == 0)”(arg2是Uint32),对吧?

我试图将函数从C ++转换为C#,但是我收到一个错误:

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

如果你能在整个功能中看到任何其他错误,我也会感激不尽。

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#

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

功能:

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

这是我还不确定的事情:

  1. C ++中的DWORD是否是UI#32或C#中的Int32?
  2. if(整数和整数)表示if(整数和整数== 0)? //这是我上面描述的错误的位置。
  3. if(!integer)表示if(integer!= 0)?
  4. 为什么运算符&不能在C#中逻辑使用,这意味着它需要一个布尔值?
  5. “LARGE_INTEGER result = {1,0}”表示result.lowpart为1且result.highpart为0或result.Quadpart = 1?

提前致谢!

你写的地方:

if(arg1&arg2 == 0)

编译器理解:

if (arg1 & (arg2==0))

你应该写:

if ((arg1 & arg2) == 0)

这是将C ++语句转换为C#的方式:

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 / C ++中,0为假,其他一切都为真。

  1. 你应该检查DWORD的定义,它应该是(unsigned int),它是C#中的UInt32
  2. if(整数和整数),在C / C ++中表示“如果按位的结果和两个整数之间的结果不是0”(0为假,其他一切都为真)。
  3. if(!integer)表示if(integer == 0)(同样,0表示false,其他一切都为true)
  4. 在C#中,就像在Java中我认为,布尔值和数字是两个不同的东西,你只能在“if”语句中使用布尔值,如果使用int则没有隐式转换:它不会编译。
  5. 我会把这个留给别人,我需要测试以确定......
  1. DWORD在C ++中是uint32_t ,因此在C#中是UInt32
  2. if(a & b)转换为if((a & b) != 0) !=之前进行评估&因此&表达式需要围绕它的括号。
  3. if(x)转换为if(x != 0)
  4. &在C#中是“按位和”,就像在C ++中一样。
  5. 取决于您的C ++结构。

5 - 这意味着两者。 因为LowPart和HighPart只是QuadPart内存的“窗口”,当result.LowPart == 1和Result.HighPart == 0时,result.QuadPart将等于1。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM