0
0
C Sharp (C#)programming~10 mins

Flags attribute and bitwise enums in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Flags attribute and bitwise enums
Define enum with Flags attribute
Assign combined flags using bitwise OR
Check flags using bitwise AND
Use flags in conditions or output
This flow shows how to define an enum with Flags, combine values using bitwise OR, and check flags using bitwise AND.
Execution Sample
C Sharp (C#)
using System;
[Flags]
enum Days { None=0, Mon=1, Tue=2, Wed=4 }
class Program {
    static void Main() {
        Days meeting = Days.Mon | Days.Wed;
        bool hasTue = (meeting & Days.Tue) == Days.Tue;
        Console.WriteLine(meeting);
        Console.WriteLine(hasTue);
    }
}
This code defines a Flags enum for days, combines Monday and Wednesday, checks if Tuesday is included, and prints results.
Execution Table
StepActionExpressionResultExplanation
1Define enum Days with FlagsDays { None=0, Mon=1, Tue=2, Wed=4 }Enum definedEnum values assigned powers of two for bitwise use
2Combine flags Mon and WedDays.Mon | Days.Wed51 (Mon) OR 4 (Wed) = 5, combined flags value
3Assign combined flags to meetingmeeting = 5meeting = 5meeting holds Mon and Wed flags
4Check if Tue flag is set(meeting & Days.Tue) == Days.Tue(5 & 2) == 2False, because 5 & 2 = 0, Tue not included
5Print meetingConsole.WriteLine(meeting)Mon, WedFlags enum prints combined names
6Print hasTueConsole.WriteLine(hasTue)FalseBoolean result of Tue check
7End--Program ends
💡 All steps executed; program ends after printing results.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
meetingundefinedundefined5 (Mon | Wed)5 (unchanged)5 (unchanged)
hasTueundefinedundefinedundefinedFalseFalse
Key Moments - 3 Insights
Why do we use powers of two (1, 2, 4, ...) for enum values?
Using powers of two ensures each flag corresponds to a unique bit, so they can be combined and checked independently as shown in step 2 and 4 of the execution_table.
Why does (meeting & Days.Tue) == Days.Tue return false when Tue is not set?
Because bitwise AND returns zero if the bit for Tue is not set in meeting. Step 4 shows (5 & 2) equals 0, so the condition is false.
How does printing the combined flags show 'Mon, Wed' instead of a number?
The Flags attribute tells C# to print combined flag names separated by commas, as seen in step 5 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the combined value of Days.Mon | Days.Wed?
A5
B3
C6
D7
💡 Hint
Check the 'Result' column in step 2 of the execution_table.
At which step does the program check if Tuesday is included in meeting?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the bitwise AND check in the execution_table.
If we add Days.Tue to meeting using bitwise OR, what would be the new value of meeting?
A6
B7
C3
D4
💡 Hint
Add the values 5 (Mon|Wed) and 2 (Tue) using bitwise OR: see step 2 for reference.
Concept Snapshot
[Flags] enum uses powers of two values
Combine flags with bitwise OR (|)
Check flags with bitwise AND (&)
Printing combined flags shows names
Use to represent multiple options in one variable
Full Transcript
This example shows how to use the Flags attribute with enums in C#. We define days with values 1, 2, 4 so each is a unique bit. We combine Monday and Wednesday using bitwise OR to get 5. We check if Tuesday is included by bitwise AND with 2, which returns false. Printing the combined flags shows 'Mon, Wed'. This technique lets us store multiple options in one variable and check them easily.