This is a SO question. Consider using something like 1<<0, 1<<1, 1<<2, and so on. When you have many flags, it becomes more readable, more maintainable, less error-prone. For instance, if you are packing all 64 bits of a 64 bit int, you really want to avoid typos :) How you represent 1 is also important. For a 64 bit integer in VS2010 I think it is 1UI64, or something like that. Using a wrong type might bite you.
–
JobDec 5 '10 at 14:08

2

@Job: Not a StackOverflow question, because it's asking about readability, recognizability, preferences and best practices. No single objective answer for it; it belongs here.
–
MacneilDec 26 '10 at 16:32

2 Answers
2

Bitwise-OR.

Addition is dangerous.

Consider an example where a bandit is a person, and an angry bandit is a bandit that speaks and shoots. Later, you decide all bandits should shoot, but you've forgotten about the angry bandit definition and don't remove its shooting flag.

+1 indeed I also think that OR makes it more clear that those are flags, but concerning the efficiency there are languages where bitwise operations are slow, e.g. JavaScript all Numbers are 64 floats bitwise operators need to do implicit conversion on those.
–
Ivo WetzelDec 5 '10 at 6:17

1

Given the OP's example, I don't think one line of ORs or addition is going to adversely impact the execution speed of a program.
–
the Tin ManDec 5 '10 at 7:33

1

@Greg: especially since the calculation in that example will be done at compile time. :-)
–
Carson63000Dec 5 '10 at 7:43

"should" is a very big word in this business. While it is highly unlikely that you will encounter this issue today, I have very clear memories of working on a processor that did not have a bitwise-OR instruction. You could bitwise-AND in one instructn, and you could bitwise-XOR in one instruction, but bitwise-OR took two: an immediate bitwise-AND to turn the bit off, and an immediate bitwise-XOR to complement the newly-cleared bit, which of course set it.
–
John R. StrohmDec 5 '10 at 14:38