tags:

views:

942

answers:

10

Given a case where I have an object that may be in one or more true/false states, I've always been a little fuzzy on why programmers frequently use flags+bitmasks instead of just using several boolean values.

It's all over the .NET framework. Not sure if this is the best example, but the .NET framework has the following:

public enum AnchorStyles
{
    None = 0,
    Top = 1,
    Bottom = 2,
    Left = 4,
    Right = 8
}

So given an anchor style, we can use bitmasks to figure out which of the states are selected. However, it seems like you could accomplish the same thing with an AnchorStyle class/struct with bool properties defined for each possible value, or an array of individual enum values.

Of course the main reason for my question is that I'm wondering if I should follow a similar practice with my own code.

So, why use this approach?

  • Less memory consumption? (it doesn't seem like it would consume less than an array/struct of bools)
  • Better stack/heap performance than a struct or array?
  • Faster compare operations? Faster value addition/removal?
  • More convenient for the developer who wrote it?
+2  A: 

It is for speed and efficiency. Essentially all you are working with is a single int.

if ((flags & AnchorStyles.Top) == AnchorStyles.Top)
{
    //Do stuff
}
ChaosPandion
That's a pretty high-level answer. Can you be specific about what operations are faster/more efficient and why? Or link to an article that justifies your claim?
Winston Fassett
Do I really need to give you proof that working with native types and simple logic expressions is fast and efficient?
ChaosPandion
Don't forget the order of operations. You have to put parenthesis around the bitwise operation there.
280Z28
Nice catch I am used to Visual Studio having my back.
ChaosPandion
+9  A: 
  • Easy setting multiple flags in any order.

  • Easy to save and get a serie of 0101011 to the database.

Jan Jongboom
That second reason is why we still use bitmasks in our C# app. It is a lot easier to have a column of int in the database that can handle up to 32 different booleans than it is to have a bool for each possible state.
Joshua
+6  A: 

Among other things, its easier to add new bit meanings to a bitfield than to add new boolean values to a class. Its also easier to copy a bitfield from one instance to another than a series of booleans.

Jherico
It seems to me that adding boolean values to a class is as easy as:bool newState;Regarding copying, it seems just as easy to copy a struct.
Winston Fassett
@Winston: Serialization format changes, and good serializers that accept default values for old data, and where old versions do not throw away unknown fields are hard to find. The binary interface changes, which may cause a chain of required updates, and requires full verisoning support for the structure. (of course the *contract* would have to state explicitely "unknown bits are ignored" or "unknown bits cause an error"). Also, on implementation level, handling them as a whole IS easier.
peterchen
@Winston what if you have created an API? Then everybody that might upgrade to your new version would have to change there code because a new bool was added to a method. While if it was an enum then no changes on there end must be made to keep there same code using it. Which is why the .NET framework favors enums over booleans.
David Basarab
@David - I wasn't talking about adding arguments to methods, but rather adding bool fields to structs, which wouldn't affect any calling methods, but to @Peter's point, would affect serialization.
Winston Fassett
+4  A: 

Actually, it can have a better performance, mainly if your enum derives from an byte. In that extreme case, each enum value would be represented by a byte, containing all the combinations, up to 256. Having so many possible combinations with booleans would lead to 256 bytes.

But, even then, I don't think that is the real reason. The reason I prefer those is the power C# gives me to handle those enums. I can add several values with a single expression. I can remove them also. I can even compare several values at once with a single expression using the enum. With booleans, code can become, let's say, more verbose.

Rui Craveiro
There are 256 combinations, but only 8 flags. Don't confuse them.
Dykam
Sorry, my English wasn't clear enough. You're right.
Rui Craveiro
+6  A: 

It was traditionally a way of reducing memory usage. So, yes, its quite obsolete in C# :-)

As a programming technique, it may be obsolete in today's systems, and you'd be quite alright to use an array of bools, but...

It is fast to compare values stored as a bitmask. Use the AND and OR logic operators and compare the resulting 2 ints.

It uses considerably less memory. Putting all 4 of your example values in a bitmask would use half a byte. Using an array of bools, most likely would use a few bytes for the array object plus a long word for each bool. If you have to store a million values, you'll see exactly why a bitmask version is superior.

It is easier to manage, you only have to deal with a single integer value, whereas an array of bools would store quite differently in, say a database.

And, because of the memory layout, much faster in every aspect than an array. It's nearly as fast as using a single 32-bit integer. We all know that is as fast as you can get for operations on data.

gbjbaanb
+2  A: 

It can also make Methods clearer. Imagine a Method with 10 bools vs. 1 Bitmask.

Michael Stum
+1  A: 

I would suggest never using enum flags unless you are dealing with some pretty serious memory limitations (not likely). You should always write code optimized for maintenance.

Having several boolean properties makes it easier to read and understand the code, change the values, and provide Intellisense comments not to mention reduce the likelihood of bugs. If necessary, you can always use an enum flag field internally, just make sure you expose the setting/getting of the values with boolean properties.

Brian
+2  A: 

Raymond Chen has a blog post on this subject.

Sure, bitfields save data memory, but you have to balance it against the cost in code size, debuggability, and reduced multithreading.

As others have said, its time is largely past. It's tempting to still do it, cause bit fiddling is fun and cool-looking, but it's no longer more efficient, it has serious drawbacks in terms of maintenance, it doesn't play nicely with databases, and unless you're working in an embedded world, you have enough memory.

Tom Ritter
Raymond is talking about bitfields, not bitmasks.
gbjbaanb
A: 

From a domain Model perspective, it just models reality better in some situations. If you have three booleans like AccountIsInDefault and IsPreferredCustomer and RequiresSalesTaxState, then it doesnn't make sense to add them to a single Flags decorated enumeration, cause they are not three distinct values for the same domain model element.

But if you have a set of booleans like:

 [Flags] enum AccountStatus {AccountIsInDefault=1, 
         AccountOverdue=2 and AccountFrozen=4}

or

  [Flags] enum CargoState {ExceedsWeightLimit=1,  
         ContainsDangerousCargo=2, IsFlammableCargo=4, 
         ContainsRadioactive=8}

Then it is useful to be able to store the total state of the Account, (or the cargo) in ONE variable... that represents ONE Domain Element whose value can represent any possible combination of states.

Charles Bretana
+1  A: 
  1. Space efficiency - 1 bit
  2. Time efficiency - bit comparisons are handled quickly by hardware.
  3. Language independence - where the data may be handled by a number of different programs you don't need to worry about the implementation of booleans across different languages/platforms.

Most of the time, these are not worth the tradeoff in terms of maintance. However, there are times when it is useful:

  1. Network protocols - there will be a big saving in reduced size of messages
  2. Legacy software - once I had to add some information for tracing into some legacy software.

Cost to modify the header: millions of dollars and years of effort. Cost to shoehorn the information into 2 bytes in the header that weren't being used: 0.

Of course, there was the additional cost in the code that accessed and manipulated this information, but these were done by functions anyways so once you had the accessors defined it was no less maintainable than using Booleans.

Larry Watanabe