c sharp

How to Use Enum Flags for Bitwise Operations in C#

Boolean conditions play a crucial role in implementing a flexible logic in our applications. As a developer, we do not need to introduce you to Boolean values.

However, when you get into complex application implementation, you may need to go beyond the true/false values.

One of the most powerful type in C# is an enumeration type which is commonly known as Enum. An Enum defines a set of named constants of underlying numerical type.

We can combine the Enum flags and bitwise operations to create complex Boolean conditions into a single variable.

We can do this by adding the Flag attribute to an Enum type which essentially turns it into a bit type. With that, we can use the bitwise operations to manipulate the values that are stored in the Enum.

In this tutorial, we will learn all about Enum flags and how to carry out the bitwise operations on the Enum values.

Basic Enum

To define an enumeration type, we use the “Enum” keyword and specify the name of the Enum members as follows:

public enum FilePermissions
{
   _ = 0,
    r = 1,
    w = 2,
    e = 4
}

C# Enum Flags

To use an Enum flag, we basically define a typical Enum type and assign the “Flags” attribute to it.

The role of the “Flags” attribute is to tell the compiler that the Enum type is to be treated as a bit field. Each field in the Enum should be a power of two as it represents a specific bit position in binary.

An example is as follows:

[Flags]
public enum FilePermissions
{
   _ = 0,
    r = 1,
    w = 2,
    e = 4
}

The given example code defines an Enum type called “FilePermissions” with the “Flags” attribute. We can use this type to represent the basic file permissions.

Bitwise Operations

Bitwise operation plays a crucial role in the way we can manipulate an Enum bit. As you can guess, we use the bitwise operator such as OR, AND, XOR, and NOT.

Bitwise OR

A Bitwise OR allows us to combine the flags which results into a single value. Take the following example code:

class Program
{
    static void Main(string[] args)
    {
        FilePermissions permissions = FilePermissions.r | FilePermissions.w;
        Console.WriteLine(permissions);
    }
}

In this case, we assign both the read and write permissions to the “permissions” variable using the OR operator.

Resulting Output:

r, w

Bitwise AND

The second bitwise operator is AND. The AND operator allows us to check if a specific flag is set.

An example is as shown:
class Program
{
    static void Main(string[] args)
    {
        FilePermissions permissions = FilePermissions.r | FilePermissions.w;
        if ((permissions & FilePermissions.r) == FilePermissions.r)
        {
            Console.WriteLine("Read permission is set.");
        }

    }
}

In the given example, we start by assigning the read and write permissions to the “permissions” variable using the OR operator.

Next, we use the AND operator to check if the “Read” permission is set. If the result of the operation is equal to the read flag, we conclude that the flag is set.

Read permission is set.

Bitwise XOR

Third, we have the XOR operator that allows us to toggle the flags. Take the following code example:

class Program
{
    static void Main(string[] args)
    {
        FilePermissions permissions = FilePermissions.r | FilePermissions.w;
        permissions ^= FilePermissions.w;
        Console.WriteLine(permissions);

    }
}

In the given example, the XOR operator toggles the write permission. If the flag is set, the operation will clear it. Otherwise, if the flag is not set, the operation will set it.

Bitwise NOT

Last of all, the NOT operator allows us to invert all the flags. Take the following example code:

class Program
{
    static void Main(string[] args)
    {
        FilePermissions permissions = FilePermissions.r | FilePermissions.w;
        permissions = ~permissions;
        Console.WriteLine(permissions);
    }

}

Inverting the flags essentially turns on all the bits that are off and the reverse is true.

Conclusion

In this tutorial, we explored how to work with Enums and Enum flags attribute to convert an Enum into a bit type. We can then perform the bitwise operations on the Enum using the bitwise operators.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list