繁体   English   中英

C++ 片段到 C# 位运算符

[英]C++ Snippet to C# Bitwise Operators

我很难将 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++;
                            }

C++, unlike C#, lets you treat an integer value as if it were a boolean value, ad-hoc, where any integer 0 is false, and any integer other than 0 is true. C# 不允许这样做。

要在 C# 中达到相同的效果,您必须明确执行我刚刚描述的检查,所以不要

if( (expr) || ... ) { }

你要

if( (expr) != 0 || ... ) { }

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

暂无
暂无

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

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