C++ Snippet到C#位运算符

Mik*_*ynn 2 c# c++ bit-manipulation

我很难将这部分C++移植到C#.我一直得到运营商'||' 不能应用于'long'和'long'类型的操作数,这是有道理的.那等价物是什么?

    while ((c <= combinations) && ((round_set & (1 << cList[c].one)) || (round_set & (1 << cList[c].two)) || (cUsed[c])))
                   {
                   fprintf( stdout, "C: %d\n", c);
                    c++;
                    }

while ((c <= combinations) && ((round_set & (1 << cList[c].one)) || (round_set & (1 << cList[c].two)) || (cUsed[c])))
                            {
                                Console.WriteLine("C: {0}", c);

                                c++;
                            }
Run Code Online (Sandbox Code Playgroud)

Duc*_*tro 6

与C#不同,C++允许您将整数值视为布尔值,ad-hoc,其中任何整数0为false,以及除0true 之外的任何整数.C#不允许这样做.

要在C#中实现相同的效果,您必须明确地执行我刚才描述的检查,而不是

if( (expr) || ... ) { }
Run Code Online (Sandbox Code Playgroud)

你要

if( (expr) != 0 || ... ) { }
Run Code Online (Sandbox Code Playgroud)

事实上后者在C++中仍然是完全可以接受的(有时为了清晰起见而鼓励).