2011-08-16 81 views
2

我很难将这部分C++移植到C#中。我不断收到Operator'||'不能应用于'long'和'long'类型的操作数,这是合理的。那么等值是什么?C++片段到C#按位运算符

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

回答

6

C++不同,C#,让你把一个整数值,就好像它是一个布尔值,即席,其中任意整数0是假的,而且比其他0任何整数都是如此。 C#不允许这样做。

为了实现在C#中同样的效果,你必须明确地执行我刚才所描述的检查,所以不是

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

你想

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

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

+0

宾果,谢谢! –