如何使用 &C#中的运算符?代码的翻译是否正确? [英] How to use the & operator in C#? Is the the translation of the code correct?

查看:38
本文介绍了如何使用 &C#中的运算符?代码的翻译是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++中的if(arg2 & 1)"行(arg2是DWORD)等于C#中的if(arg2 & 1==0)"(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#

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

功能:

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 是 UInt32 还是 C# 中的 Int32?
  2. if(integer & integer) 表示 if(integer and integer ==0)?//这是我上面描述的错误所在的地方.
  3. if(!integer) 表示 if(integer != 0)?
  4. 为什么选择运算符 &不能在 C# 中逻辑使用,这意味着它需要一个布尔值?
  5. "LARGE_INTEGER result = {1, 0}" 表示 result.lowpart 为 1,result.highpart 为 0 还是 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.

  1. 你应该检查DWORD的定义,它应该是(unsigned int),在C#中是UInt32
  2. if (integer & integer),在 C/C++ 中的意思是如果两个整数之间按位计算的结果不为 0"(0 为假,其他一切为真).
  3. if (!integer) 表示 if (integer == 0)(同样,0 为假,其他均为真)
  4. 在 C# 中,就像我认为在 Java 中一样,布尔值和数字是两个不同的东西,你只能在if"语句中使用布尔值,如果使用 int 则没有隐式转换:它不会编译.
  5. 我会把这个留给其他人,我需要测试以确保...

这篇关于如何使用 &amp;C#中的运算符?代码的翻译是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆