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

后端 未结 10 2099
猫巷女王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 04:18

    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.

提交回复
热议问题