How to Compare Flags in C?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Despite the short title, this topic is really about flag-style enums in C#. Comparing flags is different from comparing ordinary enum values because one variable can contain several options at the same time.
Define Flags With Powers of Two
A flags enum should assign each option its own bit. That means powers of two rather than consecutive integers.
None = 0 is important because it gives the enum an unambiguous empty state.
To combine flags, use the bitwise OR operator.
That value now contains two independent bits.
Check Whether a Flag Is Present
The standard comparison pattern is bitwise AND followed by equality with the flag you are testing.
This works because AND keeps only the overlapping bits. If the requested flag is present, the result matches that flag exactly.
Exact Equality Is a Different Question
A common bug is confusing "contains this flag" with "is exactly this one flag." Those are not the same comparison.
The first result is true because the Read bit exists. The second result is false because the value contains more than just Read.
Use HasFlag When Readability Matters
C# also provides HasFlag, which can make code easier to read.
For most application code this is perfectly fine. In performance-sensitive loops, some developers prefer the explicit bitwise expression because it makes the rule visible and avoids extra overhead.
Add and Remove Flags Safely
Flag comparisons are only part of the story. You also need to add and remove bits correctly.
The key operations are:
- '
|to add a flag' - '
&plus comparison to test a flag' - '
& ~flagto remove a flag'
You can wrap these rules in helpers if the enum is used heavily.
That keeps calling code clean and reduces copy-paste mistakes.
Composite Flags Are Still Flags
Some applications define reusable combinations such as editor or owner roles.
These are still just bit combinations, so the same comparison rules apply.
Common Pitfalls
The biggest mistake is defining enum values as 1, 2, 3, 4 instead of powers of two. Once the binary representation overlaps, flag checks become unreliable.
Another common issue is using == to test presence when you really need a contains check. Developers also sometimes forget to define None = 0, which makes default values much harder to reason about.
Summary
- Use
[Flags]and powers-of-two values for flag enums in C#. - Combine flags with
|and test presence with&orHasFlag. - Equality and containment are different comparisons.
- Remove flags with
value & ~flag. - Keep
None = 0so the empty state is explicit and predictable.
Related reading
- How to compare the performance of Android Apps written in Java and Xamarin C#? Anyway to check quantitative data (code & results)
- How to compare two Dictionaries in C
- How to completely uninstall Visual Studio 2010?
- How to concatenate two IEnumerableT into a new IEnumerableT?
- How to configure log4net programmatically from scratch no config
- How to connect ASP.Net Core to a SQL Server Docker container on Mac
- How to consolidate date ranges in a list in C
- How to consume a Scoped service from a Singleton?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.