Flags Enums and Bitwise Combinations
Combine enum values with the Flags attribute.
Flags Enums and Bitwise Combinations is a free C# Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
Flags Enums
A flags enum represents a combination of options. Mark it with the [Flags] attribute and give each member a distinct power of two.
using System;
[Flags]
enum Access { None = 0, Read = 1, Write = 2, Execute = 4 }
class Program {
static void Main() {
Access a = Access.Read | Access.Write;
Console.WriteLine(a);
}
}Powers of Two
Each flag must occupy a unique bit: 1, 2, 4, 8, 16. This lets values combine without overlap.
using System;
[Flags]
enum Perm { Read = 1, Write = 2, Delete = 4, Share = 8 }
class Program {
static void Main() {
Console.WriteLine((int)Perm.Share);
}
}Combining with OR
Use bitwise OR (|) to combine flags into one value.
using System;
[Flags]
enum Access { None = 0, Read = 1, Write = 2, Execute = 4 }
class Program {
static void Main() {
Access a = Access.Read | Access.Execute;
Console.WriteLine((int)a);
}
}Checking with HasFlag
HasFlag tests whether a combined value contains a specific flag.
using System;
[Flags]
enum Access { None = 0, Read = 1, Write = 2, Execute = 4 }
class Program {
static void Main() {
Access a = Access.Read | Access.Write;
Console.WriteLine(a.HasFlag(Access.Write));
Console.WriteLine(a.HasFlag(Access.Execute));
}
}Checking with Bitwise AND
You can also test a flag with & and compare to non-zero, which is fast and explicit.
using System;
[Flags]
enum Access { None = 0, Read = 1, Write = 2 }
class Program {
static void Main() {
Access a = Access.Read | Access.Write;
bool canWrite = (a & Access.Write) != 0;
Console.WriteLine(canWrite);
}
}Removing a Flag
Clear a flag with AND of the complement: a &= ~Access.Write.
using System;
[Flags]
enum Access { None = 0, Read = 1, Write = 2, Execute = 4 }
class Program {
static void Main() {
Access a = Access.Read | Access.Write;
a &= ~Access.Write;
Console.WriteLine(a);
}
}Adding a Flag
Add a flag by OR-ing it in. If it is already present, nothing changes.
using System;
[Flags]
enum Access { None = 0, Read = 1, Write = 2, Execute = 4 }
class Program {
static void Main() {
Access a = Access.Read;
a |= Access.Execute;
Console.WriteLine(a);
}
}Toggling a Flag
XOR (^) flips a flag on or off in one step.
using System;
[Flags]
enum Access { None = 0, Read = 1, Write = 2 }
class Program {
static void Main() {
Access a = Access.Read;
a ^= Access.Write;
Console.WriteLine(a);
a ^= Access.Write;
Console.WriteLine(a);
}
}Why [Flags] Matters
The [Flags] attribute changes ToString to print combined names instead of a single number.
using System;
[Flags]
enum Access { None = 0, Read = 1, Write = 2, Execute = 4 }
class Program {
static void Main() {
Access a = Access.Read | Access.Execute;
Console.WriteLine(a);
}
}Defining Combined Members
You can define convenience members that are combinations, like All.
using System;
[Flags]
enum Access { None = 0, Read = 1, Write = 2, Execute = 4, All = Read | Write | Execute }
class Program {
static void Main() {
Console.WriteLine((int)Access.All);
}
}Flags in Practice
Flags model permissions, feature toggles, and option sets compactly in a single integer.
using System;
[Flags]
enum Feature { None = 0, Dark = 1, Beta = 2, Sync = 4 }
class Program {
static void Main() {
Feature f = Feature.Dark | Feature.Sync;
Console.WriteLine(f.HasFlag(Feature.Beta));
}
}Quick Check
Test your understanding of flags enums.
Recap
A [Flags] enum gives each member a unique power of two, so flags combine with | and are tested with HasFlag or &. You add with |=, remove with &= ~, and toggle with ^=.
The attribute makes ToString print combined names, and you can define convenience members like All.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Flags Enums and Bitwise Combinations” lesson free?
Yes — the full text of “Flags Enums and Bitwise Combinations” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “Flags Enums and Bitwise Combinations”?
Combine enum values with the Flags attribute. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Flags Enums and Bitwise Combinations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining and Using Enums
- Flags Enums and Bitwise Combinations
- Enum Conversions and Parsing
- Iterating Enum Values