Quick revision
The short answer
A long bitmask is the smallest and fastest option at 8 bytes, but the meaning of each bit becomes a hidden contract and it stops at 64 flags. An EnumSet is a bit vector inside too, so it keeps most of that speed while adding names and type safety, which makes it the best default in memory. A BitSet grows without limit but has no names, and a Set<String> is flexible but costs roughly a hundred times more memory. Whatever we use in memory, we should persist names or explicit, never-reused bit numbers, never the position of an enum constant.
What to remember
In this answer, we saw that a long bitmask, a BitSet and an EnumSet are all bit vectors, and that the difference lies in names, safety and limits. A long is the smallest but turns bit positions into a hidden contract, an EnumSet keeps nearly the same cost with type safety, a BitSet grows but has no names, and a Set<String> costs far more for no real benefit. We also saw that whatever we hold in memory, we must persist names or explicit, never-reused bit numbers rather than ordinals.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A long bitmask is the smallest and fastest option at 8 bytes, but the meaning of each bit becomes a hidden contract and it stops at 64 flags. An EnumSet is a bit vector inside too, so it keeps most of that speed while adding names and type safety, which makes it the best default in memory. A BitSet grows without limit but has no names, and a Set<String> is flexible but costs roughly a hundred times more memory. Whatever we use in memory, we should persist names or explicit, never-reused bit numbers, never the position of an enum constant.
Why this question is asked
Feature flags look harmless until there are millions of records, each carrying forty of them. Then the representation decides how much heap a cache needs, how fast a check runs on every request, and, most painfully, whether the data still means the same thing after someone reorders an enum. Interviewers ask this as a design question because there is no single right answer: each option trades memory, speed, safety and evolution differently. In this answer, we will recap what each of the four options is, compare their costs with rough numbers, look at the persistence trap that catches teams later, and then decide what to use where.
1A Quick Recap of the Four Options
A long bitmask is a plain 64-bit number where each bit means one flag. We set a flag with OR, clear it with AND NOT and test it with AND. A BitSet is a JDK class that does the same over an internal long array that grows as needed, so it is not limited to 64 bits. An EnumSet is a Set implementation specialised for enum types: for an enum with up to 64 constants the JDK uses a RegularEnumSet, which stores the whole set in a single long, and for more it uses a JumboEnumSet backed by a long array. Finally, a Set<String> is an ordinary HashSet of flag names.
So three of the four options are bit vectors underneath. The real difference between them is what sits on top: raw numbers, an unnamed growable bit array, or named, typed constants.
2What Each Option Looks Like in Code
Here is the same check, is the beta search enabled for this user, written four ways.
Notice that only the EnumSet version can be read without a separate document. With the long and the BitSet, the knowledge that bit 1 means beta search lives somewhere else, usually in constants that someone must keep in sync. With the Set<String>, the names are readable but the compiler cannot check them.
3Comparing Memory, Speed and Safety
The numbers below are rough sizes on a 64-bit JVM with compressed references, for a record with all 40 flags set. They are estimates to show the scale, and a tool like JOL will give exact numbers for a given JVM. For ten million cached user records, 8 bytes per record is about 80 MB, around 32 bytes is about 320 MB, and over a kilobyte is well over 10 GB.
Speed follows the same order. A long check is one AND instruction. An EnumSet.contains reads the ordinal and does the same AND, with a method call that the JIT usually inlines. A BitSet adds an array index and a bounds check. A HashSet must compute or fetch the string's hash, find a bucket and call equals, which is many times slower in a hot path.
| Option | Approx. size | Check cost | Names and type safety | Limit |
|---|---|---|---|---|
| long bitmask | 8 bytes | one AND | none, bits are a hidden contract | 64 flags |
EnumSet | about 32 bytes | AND after an inlined call | full, compile-time checked | none (Jumbo above 64) |
BitSet | about 48 bytes | array access and AND | none | grows as needed |
Set<String> | 1 to 2 KB | hash, bucket, equals | names, but typos compile | none |
4The Persistence Trap
The most expensive mistake is not in memory, it is in storage. It is tempting to save an EnumSet to the database as a long built from each constant's ordinal(), the position of the constant in the enum declaration. It works perfectly until someone inserts a new constant in the middle of the enum or sorts the constants alphabetically. Every ordinal after that point shifts by one, and every stored value now means something different. Nothing fails; users simply get the wrong features.
The same thing happens with a hand-written long bitmask if a developer renumbers the constants. So at the boundary, in the database and in messages, we store either the names, or an explicit bit number that is part of each constant and never changes or gets reused.
Notice that the conversion uses f.bit, not f.ordinal(). Reordering the constants now changes nothing on disk. Storing names, for example as a text array or a JSON list of strings, is even safer and easier to read in a query, at the cost of a few more bytes per row.
5What We Would Choose and Why
For forty flags in a high-volume service, we would use an EnumSet in the domain model. It gives almost the memory and speed of a raw long, the compiler checks every flag name, and adding the forty-first flag is just a new constant. We would keep a raw long only inside a measured hot path or a compact binary format, wrapped in a small class so the bit numbers are defined in one place.
At the persistence and messaging boundary we would convert explicitly: names where readability and evolution matter, or the explicit bit numbers where size really matters, with a test that pins every constant's bit number. We would avoid BitSet here because it gives up names for flexibility we do not need, and Set<String> because its cost and its unchecked names buy us nothing over an enum. One more detail: EnumSet is mutable and not thread-safe, so a record shared between threads should hold an unmodifiable copy.
6Follow-up questions
When is a BitSet the right choice?
When the bits are indexes into data rather than named flags, and the count is large or not known in advance. Examples are marking which of a million ids we have seen, or a bloom filter's bit array. There names would make no sense, and BitSet's growth and bulk operations such as and, or and cardinality are exactly what we need.
What happens to an EnumSet when the enum grows past 64 constants?
EnumSet.noneOf picks the implementation from the size of the enum, so it quietly switches to JumboEnumSet, which uses a long array. Our code does not change. Any hand-written conversion to a single long, however, would break, which is another reason to persist names or explicit bit numbers rather than one long.
How would you make the flags immutable for a shared record?
Copy the EnumSet and wrap it, for example Collections.unmodifiableSet(EnumSet.copyOf(flags)), or use Set.copyOf, which returns an immutable set but loses the EnumSet's compact representation. For a hot path, the wrapped EnumSet keeps the speed while preventing changes.
7Conclusion
In this answer, we saw that a long bitmask, a BitSet and an EnumSet are all bit vectors, and that the difference lies in names, safety and limits. A long is the smallest but turns bit positions into a hidden contract, an EnumSet keeps nearly the same cost with type safety, a BitSet grows but has no names, and a Set<String> costs far more for no real benefit. We also saw that whatever we hold in memory, we must persist names or explicit, never-reused bit numbers rather than ordinals.
Open with
I would start by pointing out that three of the four options are bit vectors underneath. A long bitmask is eight bytes and a single AND to check, but the meaning of each bit lives outside the code and it caps out at 64.
Then, if they want more
An EnumSet with up to 64 constants is a RegularEnumSet, which stores the set in one long, so I get almost the same speed and memory, around 32 bytes per set, plus names and compile-time checking. A BitSet grows past 64 but has no names and is mutable.
A Set of strings is the most flexible but costs on the order of a kilobyte per record with forty flags, it hashes on every check, and a typo compiles. For a high-volume service I would use an EnumSet in the domain model, and keep a raw long only in a measured hot path behind one wrapper class.
The part I would stress most is persistence: I would never store ordinals, because reordering the enum silently changes what stored data means. I would store flag names, or give each constant an explicit bit number that is never reused, and pin those numbers with a test.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You need to store 40 independent feature flags per user record in a high-volume service....": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/33e1dbca7a/