9

I've the given condition from a cpp source.

if (!(faces & activeFace) || [...]) { ... }

I want to translate this into C#.

When I understand this right, this means as much as if activeFace is *not* in faces then... - not?

So what would be the equivalent in C#?
Note: I can't use faces.HasFlag(activeFace)

Well it should be

if ((faces & activeFace) == 0 || [...]) { ... }

Am I right?

For the completeness here the actual Flag enum

[Flags]
enum Face {
    North = 1,
    East = 2,
    South = 4,
    West = 8,
    Top = 16,
    Bottom = 32
};

Well It's the same in cpp, you just need to add a [Flags] attribute in C#

boop
  • 7,413
  • 13
  • 50
  • 94
  • Removed your awkward line. Not relevant and imho the question is not awkward but something good to know for beginners – jgauffin Aug 04 '14 at 19:44
  • What data types are faces and activeFace? – T_D Aug 04 '14 at 19:46
  • 1
    `Am I right?` Yes. Question solved? – Jashaszun Aug 04 '14 at 19:47
  • 3
    this is fairly easy to test. Did you try anything prior to asking? – default Aug 04 '14 at 19:51
  • You don't actually have to add the `[Flags]` attribute in C#. Your code will work just fine without it. – Jashaszun Aug 04 '14 at 19:53
  • @Default I've never used cpp. And on windows it's not as easy as `apt-get install gcc ; gcc test.cpp` (or whatever the actual command would be) - so: no. – boop Aug 04 '14 at 19:53
  • @Brettetete FYI, there are online compilers to easily test code, for instance http://ideone.com/ or https://dotnetfiddle.net. – default Aug 04 '14 at 19:55
  • @Jashaszun: It is a good practice in C# to add a `FlagsAttribute` when the enum is used as flag, i.e. if its constants are powers of 2, as this expresses the intention of the programmer. The attribute also influences the way `ToString` works and produces a neat output if flags are combined. This is useful when debugging. – Olivier Jacot-Descombes Aug 04 '14 at 20:22
  • @OlivierJacot-Descombes Oh wow! I've been programming in C# for years and I didn't know that `[Flags]` changed `ToString()`! I guess you learn something new every day. – Jashaszun Aug 04 '14 at 20:36

1 Answers1

5

I would add a value None = 0 to the enum

[Flags]
enum Face {
    None = 0,
    North = 1,
    East = 2,
    South = 4,
    West = 8,
    Top = 16,
    Bottom = 32
};

and then test

if ((faces & activeFace) == Face.None || otherExpr) {
    ...
}

A good reason to add a 0 constant to an enum is that class fields are zeroed by default and omitting a 0 constant would lead to enum values not corresponding to any enum constant. It is legal in C# to do that, but it's not a good practice. C# does not test whether values assigned to enums are valid enum constants.

But if you cannot change the enum, you can cast the enum value to int

if ((int)(faces & activeFace) == 0 || otherExpr) {
    ...
}

And yes, in C++ any int unequal 0 is considered as Boolean true value, so !(faces & activeFace) in C++ means: activeFace is not in faces

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Well, it seems you put all the other answers out of contention. Thanks for your extensive answer, here your well deserved +1 ;) – boop Aug 05 '14 at 16:25