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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/685436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:24:02  来源:igfitidea点击:

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

c#operatorsbitwise-operatorsconditional-statements

提问by Ivan Prodanov

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

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

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

我正在尝试将函数从 C++ 转换为 C#,但出现错误:

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++

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#

转换为 C#

LARGE_INTEGER structure:

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

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:

这是我还不确定的:

  1. Whether a DWORD in C++ is UInt32 or Int32 in C#?
  2. if(integer & integer) means if(integer and integer ==0)? //this is where the error i described above is placed.
  3. if(!integer) means if(integer != 0)?
  4. Why operator & cannot be used logically in C# ,meaning it requires a boolean?
  5. "LARGE_INTEGER result = {1, 0}" means result.lowpart is 1 and result.highpart is 0 or result.Quadpart = 1?
  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?

Thanks in advance!

提前致谢!

采纳答案by ybo

Where you write :

你在哪里写:

if (arg1 & arg2==0)

如果 (arg1 & arg2==0)

The compiler understands :

编译器理解:

if (arg1 & (arg2==0))

You should write :

你应该写:

if ((arg1 & arg2) == 0)

This is the way the C++ statement should be translated to C# :

这是将 C++ 语句转换为 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#


In C/C++, 0 is false, everything else is true.

在 C/C++ 中,0 为假,其他均为真。

  1. You should check the definition of DWORD, it should be (unsigned int), which is UInt32 in C#
  2. 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).
  3. if (!integer) means if (integer == 0) (again, 0 is false, everything else is true)
  4. 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.
  5. I'll leave this one to someone else, I'd need to test to be sure...
  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. 我会把这个留给别人,我需要测试以确保......

回答by strager

  1. DWORDis uint32_tin C++, thus UInt32in C#.
  2. if(a & b)converts to if((a & b) != 0). !=is evaluated before &thus the &expression needs parentheses around it.
  3. if(x)converts to if(x != 0)
  4. &is a 'bitwise and' in C#, like in C++.
  5. Depends on your C++ structure.
  1. DWORDuint32_t在C ++,因此UInt32在C#。
  2. if(a & b)转换为if((a & b) != 0). !=之前被评估,&因此&表达式需要在它周围加上括号。
  3. if(x)转换为 if(x != 0)
  4. &在 C# 中是一个“按位与”,就像在 C++ 中一样。
  5. 取决于您的 C++ 结构。

回答by Jim Mischel

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.

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