views:

187

answers:

5

In C and embedded, one frequently uses enumerated constants where every value is a bit mask with exactly 1-bit set. (e.g. 0x0001, 0x0002, 0x0004, etc.) Is there a standard name for this type of bitmask? I've seen them referred to as flags, but more in passing than as a standard definition. I know it sounds snobbish, but "flags" doesn't really seem technical enough? Does anyone else have a good name for these? I can't imagine that noone has come up with one.

+10  A: 

"Flags" is the accepted term. "Pass down a flag", "set this flag", etc.

MSN
The Wikipedia entry for Flags says this: flag refers to one *or more* bits that are used to store a binary value *or code*. This is exactly why I want something less generic.
ktrimbach
You can't always get what you want. "Flag" is the standard term for this feature.
Jonathan Grynspan
@ktrimbach, the wikipedia entry for flag word indicates that the name strongly implies a direct mapping of a single bit to a single flag. And I have never heard the term used to indicate multiple bits.
MSN
+2  A: 

The .NET framework has a Flags attribute that is used to indicate exactly the behavior you're describing.

They don't exactly just make up names for whatever they want, so a flag is probably the most acceptable term.

Mark Rushakoff
This is a good reference. .NET does define a FlagAttribute for enums and MSDN does describe it kind of like this. I'm not sure what good it does other than allowing ToString() to concat multiple enums together, but it is illustrative.
ktrimbach
+2  A: 

I've also seen "bit flags".

egrunin
I've found a Dr Dobbs article that mentions "Bit Flag Enumerations". That's a bit ('scuse pun) closer to what I'm thinking. http://www.drdobbs.com/184403893
ktrimbach
+3  A: 

"Flag" is used for these for decades now and is just fine. My old C64 already had a zero flag, carry flag, etc. See here for further info:

http://en.wikipedia.org/wiki/Flag_(computing%29

http://en.wikipedia.org/wiki/Status_register

I don't know exactly when this term was coined and by whom.

Secure
The Wikipedia link specifically talks about status registers and Flag words.These are hardware bit flags. That's probably where it came from, but I'm looking for something more.
ktrimbach
@ktrimbach: The first link (I've edited my post so now it works) also says: 'In other cases, the binary values may represent one or more attributes in a bit field, often related to abilities or permissions, such as "can be written to" or "can be deleted".'
Secure
+1  A: 

In mathematics, a singleton set is a set with exactly one element, so you could conceivably call them a singleton bitset or singleton bitmask, but I haven't seen this in practice.

ergosys
Answering the wrong question?
Joshua
@Joshua, not sure what you are referring to.
ergosys
The value doesn't have one bit set, the list of combined values each have one bit set.
Joshua
I read the question as asking about the individual constants. I don't see how it can be parsed any other way. Look at the question title for example, or read the first two sentences. Honestly, I don't understand where you are coming from.
ergosys
Good reference. I would guess that a more academic analysis might incorporate that term/concept. However, Singleton is already a well-known design pattern in OOD, so I'm not sure if I could get away using it for this.
ktrimbach