I rewrite code from C to C #. Met cycle

// инициализация a и b while (a & b) { ... } 

With such an entry in c # VS displays

implicit conversion of type 'int' to 'bool' is impossible

I understand that in C there, most likely, just a single vector should be, but how correct it is to remake it with c #?

    3 answers 3

    If you directly rewrite it, it will be:

     while((a & b) != 0) { } 

    I mean, the cycle will not end until all the bits disperse in both variables. And so without a context it is difficult to say, let's say inside the loop you just assign a = 0. Then you can replace with bool and remove! = 0.

    • Well, yes, I just thought it could be somehow beautiful. - Setplus
    • To make it beautiful, you need to understand what is inside the loop - arndtdv
    • @Setplus: To make it beautiful, you should not rewrite one-to-one, but use language-specific features. For example, type bool or LINQ there. - VladD

    Hello. You run the cycle and indicate that "A and B", but there is no condition. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/while

    • 3
      The fact of the matter is that in the C language this is a valid entry. Unlike C-Sharp. - Alexander Petrov

    In C #, there is a separate boolean type, and other integer types cannot be used implicitly. In C, on the contrary, there is no Boolean type, and any integer types are used. And the most important thing to remember is that in C, only 0 is false (false), everything else is true (true). When porting C-code to C #, you need to look - if the condition does not check for equality, but just an integer expression, then you need to add a code in C # "! = 0" to it.