Combinable enumerations in C# .NET: numeric values of large enums

In this post we saw how to build an enumeration that can be combined using the bitwise OR, i.e. | operator:

var frontEndProgrammer = SoftwarePositions.Programmer | SoftwarePositions.FrontEnd;

We saw that enumeration values must be set in powers of two:

[Flags]
public enum SoftwarePositions
{
	Architect = 0
	, Developer = 1
	, Programmer = 2
	, FrontEnd = 4
	, BackEnd = 8
	, Database = 16
	, DomainModeller = 32
}

This is fine for short enumerations where it’s visibly easy to follow the numeric values 0,1,2,…,32. Most programmers are familiar with these digits so they’ll know what you are doing.

However, if this list gets longer then the powers of 2 will also grow exponentially. Here’s a table from Wikipedia:

Powers of two table from wikipedia

So if you have 31 elements in your enumeration then its value will be 1,073,741,824 as we’re starting with 0. Surely not too many people can remember that.

There’s a trick, however, to simplify this code using the left-shift operator:

[Flags]
public enum SoftwarePositions
{
	Architect = 0
	, Developer = 1
	, Programmer = 1 << 1
	, FrontEnd = 1 << 2
	, BackEnd = 1 << 3
	, Database = 1 << 4
	, DomainModeller = 1 << 5
}

This will shift the bit set for one by as many positions as there are after the left-shift operator yielding the number we’re after. E.g. left-shifting 1…

00000001

…by 3 positions gives…

00001000

…which is 8 in binary. The enum element BackEnd has value 8 so it’s correct.

View all various C# language feature related posts here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

2 Responses to Combinable enumerations in C# .NET: numeric values of large enums

  1. Michael Schmid says:

    Hi, great trick to use shift-operation instead doing the calculating-stuff.

    But using zero as valid value seems problematic when using binary-operations to determine which flags are set. I think zero should be no flag/meaning is set.

    I’ve found an msdn-article to that topic: “Do not give a flag a value of zero if it does not mean “no flags are set”. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/enumeration-types#enumeration-types-as-bit-flags

    var myProgrammer = SoftwarePositions.Programmer;
    bool isItMyArchitect = (myProgrammer & SoftwarePositions.Architect) == SoftwarePositions.Architect;

    isItMyArchitect
    true

    Keep doing this great blog and
    Best Regards Michael Schmid

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: