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

Flags attribute and bitwise enums in C Sharp (C#)

Choose your learning style9 modes available
Introduction

The Flags attribute helps combine multiple options in one variable using bits. It makes checking and setting options easy and clear.

When you want to store multiple yes/no options in one variable.
When you need to check if certain options are set quickly.
When you want to combine settings like file permissions or user roles.
When you want to save memory by using one variable instead of many booleans.
Syntax
C Sharp (C#)
using System;

[Flags]
public enum Options
{
    None = 0,
    OptionA = 1 << 0,  // 1
    OptionB = 1 << 1,  // 2
    OptionC = 1 << 2,  // 4
    OptionD = 1 << 3   // 8
}

The [Flags] attribute tells C# to treat the enum as a set of bits.

Each option should be a power of two (1, 2, 4, 8, etc.) so they can combine without overlap.

Examples
Basic flags enum for file permissions.
C Sharp (C#)
[Flags]
public enum Permissions
{
    None = 0,
    Read = 1,
    Write = 2,
    Execute = 4
}
Example of no flags set (empty).
C Sharp (C#)
Permissions userPermissions = Permissions.None;
// Empty flags, no permissions set
Combining flags to set multiple options.
C Sharp (C#)
Permissions userPermissions = Permissions.Read | Permissions.Write;
// Combining two flags using bitwise OR
Check if a specific flag is set using bitwise AND.
C Sharp (C#)
bool canRead = (userPermissions & Permissions.Read) == Permissions.Read;
// Checking if a flag is set
Sample Program

This program shows how to add, check, and remove flags using bitwise operations with a Flags enum.

C Sharp (C#)
using System;

[Flags]
public enum AccessRights
{
    None = 0,
    Read = 1 << 0,   // 1
    Write = 1 << 1,  // 2
    Execute = 1 << 2 // 4
}

public class Program
{
    public static void Main()
    {
        AccessRights userRights = AccessRights.None;
        Console.WriteLine($"Initial rights: {userRights}");

        // Add Read and Write rights
        userRights |= AccessRights.Read | AccessRights.Write;
        Console.WriteLine($"After adding Read and Write: {userRights}");

        // Check if Execute right is set
        bool hasExecute = (userRights & AccessRights.Execute) == AccessRights.Execute;
        Console.WriteLine($"Has Execute right? {hasExecute}");

        // Remove Write right
        userRights &= ~AccessRights.Write;
        Console.WriteLine($"After removing Write: {userRights}");

        // Check if Write right is set
        bool hasWrite = (userRights & AccessRights.Write) == AccessRights.Write;
        Console.WriteLine($"Has Write right? {hasWrite}");
    }
}
OutputSuccess
Important Notes

Time complexity for checking, adding, or removing flags is O(1) because bitwise operations are very fast.

Space complexity is minimal since all flags are stored in one integer variable.

Common mistake: Not using powers of two for enum values causes flags to overlap and behave incorrectly.

Use Flags enums when you want to combine options efficiently; use regular enums when options are exclusive.

Summary

The [Flags] attribute allows combining multiple enum values using bits.

Use bitwise OR (|) to add flags, AND (&) to check flags, and AND with NOT (& ~) to remove flags.

Always assign enum values as powers of two to avoid overlap.