Why use flags+bitmasks rather than a series of booleans?

后端 未结 10 2096
猫巷女王i
猫巷女王i 2021-02-05 03:29

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 ju

相关标签:
10条回答
  • 2021-02-05 03:52
    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.

    0 讨论(0)
  • 2021-02-05 03:56

    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.

    0 讨论(0)
  • 2021-02-05 03:59
    • Easy setting multiple flags in any order.

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

    0 讨论(0)
  • 2021-02-05 04:05

    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.

    0 讨论(0)
  • 2021-02-05 04:14

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

    0 讨论(0)
  • 2021-02-05 04:15

    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.

    0 讨论(0)
提交回复
热议问题