2

I've been looking over some game programming code and have seen enums as in:

[Flags]
public enum CollisionCategories
{
    Cat1 = (1 << 0),
    Cat2 = (1 << 1),
    Cat3 = (1 << 2),
    Cat4 = (1 << 3),
    ...
}

Now, would this not be the same as just setting each item like 1, 2, 4, 8, ... ? I've seen the later as well. I know doing something like string s = string.Empty is better than string s = "" as far as performance goes but not sure about the enum.

Any thoughts?

Thanks much,

David

4
  • 4
    s = string.Empty is just more expressive that's why people are using it, this has nothing to do with performance Commented Jan 15, 2011 at 18:03
  • It is exactly the same thing... (1 << x) will be compiled to a constant so there is no performance differences. It is only because it might be easier to read (1 << 8) rather than 256. Commented Jan 15, 2011 at 18:04
  • 3
    I wish [flags] automagically gave you 1, 2, 4, 8, etc.... in place of 0, 1, 2, 3, ... rather than having to type it in manually. One can dream. Commented Jan 15, 2011 at 18:09
  • Sounds good. Thanks all for the replies. Commented Jan 15, 2011 at 22:46

7 Answers 7

3

You are correct about the values that are stored. It would not make a difference in performance, so it is a readability issue that may make more sense in the context of the code.

Sign up to request clarification or add additional context in comments.

Comments

1

It's just easier when you get to the larger numbers.

I'm sure that (1<< 24) is simpler than going to your calculator, calculating it, and pasting.

Comments

1

For some people, setting them this way is just cleaner than the typical C raw hex/decimal number initialization. (0x1,0x4000,etc) Since the compiler recognizes a literal and turns them into plain numbers, there's no performance drawback, it's just a style matter.

Comments

0

Yes it is the same.

I personally would use 1,2,4,8... because I think these numbers are so much well known that no one can misunderstand what's going on.

Performance is the same. Code size is the same.

2 Comments

Maybe the first few powers of 2 are well-known but those numbers get huge quite fast. If you have more than say 5 flags, the bit-shift representation is definitely more readable.
Yes of course. I use only for small numbers, I remember values for 18 bits. Usually this is all I need when defining my enums.
0

Yes, it is the same. It's just so the code is more readable.

Comments

0

I think String.Empty is equivalent to "" the compiler will recognise the literal and convert it..... Anyway, it's the same for you enum, it's just a question of style and in the case of the enum it makes it 100% explicit that these are bit flags I suppose.

Or maybe the coder wasn't confident with his powers of 2, and couldn't be bothered to work them out.

Comments

0

Performance is not an issue here. Enum is compiled as a constant so it is the compiler that is doing the job hence no difference in runtime performance.

Alternative could have been using the power but since that involves code, it would not work:

enum MyEnum{
  Val1 = Math.Pow(1,2) // throws compile error
}

Error 1 The expression being assigned to 'ConsoleApplication1.Program.MyEnum.Val1' must be constant C:\Users\Aliostad\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 130 11 ConsoleApplication1

Enum values must be constants.

Comments

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.