Did you know that there is a “bitwise” operator that you can use when you define subscriptions in BizTalk? I surely did not! I just stumbled upon it an since I could not find any really good article on it I thought I would give it a try:

What is the Bitwise operator?

This is certainly best answered by others; like wikipedia or this guy, or Microsoft of course.

If you want the short, short version; it is a good way to let one number represent a combination of other numbers, so that you know if a particular number is present well that did not explain a lot. Let me try a longer version.

What problem does bitwise solve?

Bitwise is often used with enums in .net and if you want to use bitwise on an enum you mark it with the [Flags] attibute. The bitwise operator is a single ampersand (&), not to be confused with another logical operator (&&).

Imagine you are designing role based access management in a web application. You decide to have three different kinds of users (or roles); standard user, project managers and administrators.
The standard user has access to information about his/her current projects. The project manager can add/remove users to projects, and lastly the administrator can add projects and users.

When a web page is accessed, the user membership is checked and the information (perhaps in tabs) is displayed to the user depending on the user’s role.

How would you solve the fact that there might be users that are both standard users and project managers (depending on project)? Or, how about a user being administrator, project manager and standard user? How do you assign one number that combines different roles and give the user a particular access number?

Of course there are a lot of ways to do this but that does not prove my point, so I will assume that the way to do it is using [Flags] enum and the bitwise operator.

The enum values

To make this work you have to assign values to your enum in a specific way: The value of the enum is double that of the last value in the series, except for the second one, which is the number one.

enum MyRoles : uint
 {
   Anon = 0,
   StdUsr = 1,
   ProjectMgr = 2,
   Admin = 4
 }

So, a standard user has an access number of 1 and a project manager has the number 2. A user that is both a standard user and a project manager; the user gets an access number of 3 (1+2 = 3). This number is unique in the series and this will hold true for all combinations, even if you would have hundreds of roles.

Based on this logic, there is an easy way to find out if a user is in a particular role by using the &-operator. If you want to know if the user is a project manager, the code is simple:

if ((MyUser.AccessNumber &
      MyRoles.ProjectMgr) == MyRoles.ProjectMgr)

If the the expression returns true, the user is a project manager.

To be clear; there is a much simpler way of defining the same expression in .Net 4.0. Here; the bitwise operator is used under the convers.

if ( MyUser.AccessNumber.HasFlag(
     MyRoles.ProjectMgr))

Continued in part 2.

Blog Post by: Mikael Sand