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.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: