2

Anyway here's my problem I have been modifying a whole C++ program to work in C# and I am pretty much done, I have this If statement in the C++ program.

if(info[i].location & 0x8 || 
             info[i].location & 0x100|| 
                     info[i].location & 0x200)
{
    //do work
}
else
{
    return
}

And of course when I do this in C# it gives me a "Operator '||' cannot be applied to operands of type 'int' and 'int'" error.

Any clue on what my problem is, Im guessing that C# has got a way of doing this as I am fairly unfamiliar with these old C operators.

2 Answers 2

10

Why it fails

Fundamentally it's the same difference as this:

if (someInteger) // C or C++

vs

if (someInteger != 0) // C#

Basically C# is a lot stricter when it comes to logical operators and conditions - it forces you to have use something which is either bool or convertible to bool.

As an aside, that's also why in C# this isn't just a warning, but a full-blown error:

int x = ...;
if (x = 10) // Whoops - meant to be == but it's actually an assignment

If you see comparisons this way round:

if (10 == x)

That's usually developers trying to avoid typos like the above - but it's not needed in C# unless you're really comparing against constant bool values.

Fixing the problem

I suspect you just need:

if (((info[i].location & 0x8) != 0)) ||
    ((info[i].location & 0x100) != 0)) ||
    ((info[i].location & 0x200) != 0)))

It's possible that you don't need all of those brackets... but another alternative is just to use one test:

if ((info[i].location & 0x308) != 0)

After all, you're just testing whether any of those three bits are set...

You should also consider using a flags-based enum:

[Flags]
public enum LocationTypes
{
    Foo = 1 << 3; // The original 0x8
    Bar = 1 << 8; // The original 0x100
    Baz = 1 << 9; // The original 0x200
}

Then you could use:

LocationTypes mask = LocationTypes.Foo | LocationTypes.Bar | LocationTypes.Baz;
if ((info[i].location) & mask != 0)

Or using Unconstrained Melody:

LocationTypes mask = LocationTypes.Foo | LocationTypes.Bar | LocationTypes.Baz;
if (info[i].location.HasAny(mask))
Sign up to request clarification or add additional context in comments.

2 Comments

Or even better--define each bit as a constant and bitwise-or them together into a mask to compare against
@DougT.: That's pretty much what my last edit was about - but instead of several integer constants, use an enum.
4

It's pretty much exactly what it says: in C#, || is a logical-only operator and so cannot work on int. You can replace || with | (the bitwise-or which works on ints), but then the entire expression will need to be converted to boolean by comparing it to zero, because in C# the if-statements require a boolean.

Alternatively, you can replace info[i].location & 0x8 with (info[i].location & 0x8 != 0).

2 Comments

+1, better answer because it explains why the compiler is giving an error.
Jon Skeet was just way faster

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.