0Pricing
C# Academy · Lesson

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 — 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

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, and the C# Academy course includes 4 lessons in total. 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.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Defining and Using Enums
  2. Flags Enums and Bitwise Combinations
  3. Enum Conversions and Parsing
  4. Iterating Enum Values
← Back to C# Academy