Tuesday, October 9, 2007

Custom Serverside paging in GridView Vs DataGrid

 When doing serverside paging(that is paging at the database layer by returning only the paged result), one of the things I miss in the GridView control is the VirtualItemCount, which is supposedly only supported in the older control's like the DataGrid.

This property was quite useful because while being able to supply to the DataGrid a variable number of paged result sets, i was also able to tell the DataGrid, the total number of records, that way it knew how many pager buttons to display.

Eg. If we had, say a total of a 100 records and had the pageSize set to 7, so only 7 records are shown at a time, then how does the grid know how many numbered pager buttons to display allowing us to navigate from one page to another ? That's where the VirtualItemCount came into play and saved the day. To this property we'd pass a total records count and that was it. In the GridView today ? There is no VirtualItemCount present. The way it were planned it seems is to use the ObjectDataSource, which in my honest opinion is simply extra work, however it does abstract much of this code nicely and put it where it should be, in the data tier.

In all the example code in this post, I shall be using the MemberShip.GetAllUsers method. This method is overloaded and can retrieve a paged result of users Versus returning all the user's in the database, which is a quite handy overload and works out nicely for the code i want to use in this post.

Let's look at a simple example of how we couldof performed custom paging on the DataGrid control back in the old days :


int virtualItemCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataGridMembers.DataSource
= Membership.GetAllUsers(0,
DataGridMembers.PageSize,
out virtualItemCount);
DataGridMembers.VirtualItemCount
= virtualItemCount;
DataGridMembers.DataBind();
}
}

Note the VirtualItemCount ? Then on the PageIndexChanged event of the DataGrid we did :

protected void DataGridMembers_PageIndexChanged(object source, 
DataGridPageChangedEventArgs e)
{
DataGridMembers.CurrentPageIndex
= e.NewPageIndex;
DataGridMembers.DataSource
= Membership.GetAllUsers(e.NewPageIndex,
DataGridMembers.PageSize,
out virtualItemCount);
DataGridMembers.DataBind();
}

And that was it, it was as simple as that.

Now try to do that on the GridView ? Can't be done and this is a control that is replacing the old DataGrid control. To make things worse, the DataGrid is not a supported control anymore in 2.0 ; It's been obsoleted and by default you wont even find this control in your toolbox. You can still use it however by manually adding it to your toolbox. Unfortunate, because there are moments like this custom paging situation and i'm getting nostalgic already.

So, how to achieve the same thing in the GridView control which happens to replace the DataGrid ? Well, it's a long shot. Since we cannot achive this directly on the GridView, we are going to have to do it via the DataSource control, which is actually the control that is populating the data for the gridview and also the control that handles paging and sorting amoung other things. While i like this kind of data abstraction, i'm actually doing more work and making the extra effort,but this is how you would implement custom paging on  your GridView control.
The GridView alone is lacking a VirtualItemCount property, which i believe shouldn't have been so hard to implement. To compensate for this lacking, you perform custom serverside paging by using an ObjectDataSource control, defining a SelectMethod and a SelectCountMethod method. The SelectCountMethod is your custom method that returns the Total records count.

So let's look at some code, and there are few gotcha's that weren't exactly obvious to me in the begining :
First our custom SelectMethod :

public MembershipUserCollection GetAllUsers(int startRowIndex, 
int maximumRows)
{
if (startRowIndex > 0)
startRowIndex
= startRowIndex / maximumRows;
return Membership.GetAllUsers(startRowIndex,
maximumRows,
out selectCountValue);
}

One gotcha you want to make note of is how i have some extra code to divide startRowIndex by maximumRows ; This is because startRowIndex is actually the first row in the resultset as the variable name indicates, however what i really need is the current page index, because that is what our stored procedure is expecting, in this case that is what the internal MemberShip.GetAllUsers method is expecting.

Next we need to add a SelectCountMethod :

int selectCountValue = 0;
public int SelectVirtualCount()
{
return selectCountValue;
}

The code is minimum as you can note, but ofcourse, there is some extra effort to making the abstraction. The code above goes into the data layer.

And lastly, we need to subscribe to PageIndexChanging event of our GridView and pass the selected page index :

protected void GridViewMembers_PageIndexChanging(object sender, 
GridViewPageEventArgs e)
{
GridViewMembers.PageIndex
= e.NewPageIndex;
}

A peculiar behaviour you will notice is that the SelectCountMethod and the SelectMethod both share the same SelectParameters if SelectParameters are defined. Peculiar because i was not really expecting it, however I have no issues with it, For example if we had to rewrite our previous example to include also a search by userName, then our ObjectDataSource would be expecting some SelectParameters like this :

<asp:ObjectDataSource ID="ObjectDataSourceMembers"
EnablePaging
="True"
SelectCountMethod
="SelectVirtualCount"
SelectMethod
="GetAllUsers"
TypeName
="MembersData"
runat
="server">
<SelectParameters>
<asp:ControlParameter ControlID="TextBoxUserName" Name="userName"
PropertyName
="Text" DefaultValue="All" />
</SelectParameters>
</asp:ObjectDataSource>

And then modified our SelectMethod as such :

public MembershipUserCollection GetAllUsers(int startRowIndex, 
int maximumRows, string userName)
{
if (startRowIndex > 0)
startRowIndex
= startRowIndex / maximumRows;
if (userName == "all")
{
return Membership.GetAllUsers(startRowIndex,
maximumRows,
out selectCountValue);
}
else
{
return (MembershipUserCollection)
Membership.FindUsersByName(userName
+ "%",
startRowIndex, maximumRows,
out selectCountValue);
}
}

As you can see while our GetAllUsers(SelectMethod) has the needed userName parameter, our SelectVirtualCount method defined above does not have any parameters defined on it, since we don't need to pass it anything. However, the ObjectDataSource is going to complain with :

ObjectDataSource 'ObjectDataSourceMembers' could not find a non-generic method 'SelectVirtualCount' that has parameters: userName

So it means both the SelectMethod and the SelectCountMethod share the same Select parameters. I resolved by adding the extra userName parameter in the SelectCountMethod as well, while i did not clearly need it, but no big deal.
Here is what the modified SelectCountMethod wouldof looked like :

int selectCountValue = 0;
public int SelectVirtualCount(string userName)
{
return selectCountValue;
}

The SelectCountMethod is treated exactly in the same way the SelectMethod is treated, so the ObjectDataSource's Selected Event is going to fire twice for example, once when the SelectMethod is called and once when the SelectCountMethod is called. These are all gotchas i was not really prepared for.

Full code for custom serverside paging in DataGrid :

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<script runat="server">
int virtualItemCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataGridMembers.DataSource
= Membership.GetAllUsers(0,
DataGridMembers.PageSize,
out virtualItemCount);
DataGridMembers.VirtualItemCount
= virtualItemCount;
DataGridMembers.DataBind();
}
}

protected void DataGridMembers_PageIndexChanged(object source,
DataGridPageChangedEventArgs e)
{
DataGridMembers.CurrentPageIndex
= e.NewPageIndex;
DataGridMembers.DataSource
= Membership.GetAllUsers(e.NewPageIndex,
DataGridMembers.PageSize,
out virtualItemCount);
DataGridMembers.DataBind();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="DataGridMembers" AllowPaging="True"
AllowCustomPaging
="true" PageSize="2" runat="server"
OnPageIndexChanged
="DataGridMembers_PageIndexChanged">
<PagerStyle Mode="NumericPages"
HorizontalAlign
="Right" />
</asp:DataGrid>
</div>
</form>
</body>
</html>

And the full code for custom paging in GridView :

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>

<script runat="server">
protected void GridViewMembers_PageIndexChanging(object sender, 
GridViewPageEventArgs e)
{
GridViewMembers.PageIndex
= e.NewPageIndex;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridViewMembers" DataSourceID="ObjectDataSourceMembers"
runat
="server" AllowPaging="True" PageSize="2"
OnPageIndexChanging
="GridViewMembers_PageIndexChanging">
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSourceMembers"
EnablePaging
="True"
SelectCountMethod
="SelectVirtualCount"
SelectMethod
="GetAllUsers"
TypeName
="MembersData"
runat
="server"></asp:ObjectDataSource>
</div>
</form>
</body>
</html>
public class MembersData
{
public MembersData()
{
//
// TODO: Add constructor logic here
//
}
int selectCountValue = 0;
public int SelectVirtualCount()
{
return selectCountValue;
}
public MembershipUserCollection GetAllUsers(int startRowIndex,
int maximumRows)
{
if (startRowIndex > 0)
startRowIndex
= startRowIndex / maximumRows;
return Membership.GetAllUsers(startRowIndex,
maximumRows,
out selectCountValue);
}
}

Thursday, October 4, 2007

Rich ajax applications that do not break if javascript is disabled.

Always wanted to test if the client's browser has javascript disabled in their browser ? Sounding impossible ? Well, not anymore!

Ok, so as Web2.0 applications are increasing in popularity and we are all starting to enable a lot of clientside javascript in our web applications, especially the use of ajax extentions and the updatepanel, we also know how this is all going to break for users who have javascript turned off or whose browsers do not support javascript.

Now, lets be real, it's not very common nowadays for a useragent to not support javascript, all the big players support it well(IE, Firefox, Opera, Safari).

However what about users that have javascript turned off ? Personally, I don't want my applications breaking on potential clients who fall in this category and realistically, today, there is no way to test for sure if the client turned off javascript. Sure, I am aware of the HttpBrowserCapabilities class, specifically the EcmaScriptVersion property. However this is a pretty useless property to me in this usage scenario since it only tells me what version of javascript the users client browser supports. This is simple static information passed on by the clientbrowser when making the request.

What we need is to know "Is javascript turned off ?" Knowing this is important in today's applications because it allows us to gracefully exit, or provide a serverside alternative, a non script version of our page being requested.

As of this writing I have not found any clear answer anywhere, so i have invested a couple of minutes to come up with the following solution. It's quite simple and should be working 100% without breaking, telling us exactly what we are seeking. -->> Hey, you there making the request, do you have javascript enabled/disabled ?

The idea is to introduce a piece of js code when the first request is being made by the client. Particularly a piece of code that can tell us serverside, "yes, javascript is enabled", without any delay or rendering much content to the client.

<script type="text/javascript">
window.location.href
='http://weblogs.asp.net/Default.aspx?supportsjs=true';
</script>

if javascript is enabled, the clients page will redirect, but what if js is disabled ? this script wont do anything, so to address this, we will introduce a second piece of code as well :

<meta http-equiv="refresh" 
content
="0;url=http://weblogs.asp.net/Default.aspx?supportsjs=false" />

That's it. If js is disabled, this second line will kick off and postback. Perfect.

Still, we don't want to do this everytime. We want to do this for the first request only, so what we can do is set a serverside flag in session state. I don't really recommend using session state because I find sessions to be unreliable, but that's just me. You basically have no control on when the session recycles. My preference lies in the profile provider(which is going to persist the value in the database and in style), however for simplicity, i'm setting a flag in session state.


Update october 4th,2007 : Initially, as you have read above in this post, i was planning on using the meta refresh tag if js was indeed disabled on the client. It was also a useless effort, when I couldof just assumed js was disabled from the start and only enabled if the js script fired the postback. As you will note from Richards comment below, the meta refresh tag only brings other problems to the table as it can in turn be disabled invidividually by the browser and not only in IE or Firefox but opera has support for this too. Here is the updated code that checks for javascript without meta refresh tag getting in the way.

Following is the code :

<%@ Page Language="C#" %>

<script runat="server">

/*
do it in init, otherwise you cannot
set ScriptManager.EnablePartialRendering property
*/
protected void Page_Init(object sender, EventArgs e)
{
bool notSet = string.IsNullOrEmpty(this.Request.QueryString["supportsjs"]);
string url = this.Request.Url.OriginalString;
if (notSet && Session["supportsjs"] == null)
{
string queryStringKey = (this.Request.QueryString.Count > 0) ?
"&amp;supportsjs=" : "?supportsjs=";

string jsTesterScript = string.Format(
"<{0} type=\"text/javascript\">window.location.href='{1}{2}true'</{0}>",
"script", url, queryStringKey);

Response.Write(jsTesterScript);
Response.Flush();
}

if (Session["supportsjs"] == null)
Session[
"supportsjs"] = false;// default, assume false

// if our js code posted back,
// and we still hold default assumption,
// then client does indeed support js
// so update the session

if (!notSet && !(bool)Session["supportsjs"])
Session[
"supportsjs"] = true;
ScriptManager1.EnablePartialRendering
= (bool)Session["supportsjs"];
}

</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<h1>outside updatepanel : <%= DateTime.Now %></h1>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<h1>Inside updatepanel : <%= DateTime.Now %></h1>
<asp:Button ID="Button1" runat="server" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

I have tested the above code to work flawlessly in IE, Firefox, Opera and Safari. I've enabled and disabled javascript in the browsers during testing and as simple as the code may seem, it works. This is indeed great because now your rich ajax web applications can cater nicely to all targets and not break in the face of who has javascript disabled.

Tuesday, October 2, 2007

One bit masks for Access Control (setting permissions) in your asp.net applications.

In my previous post, i wrote about a brushup on four binary bitwise operators. In this post, i would like to cover how we can make use of binary bitwise operators to create 1 bit masks, working at the bit level, saving us lots of memory, while also being easy to read and maintain our code. First, lets see how the bitwise operators we covered previously can be used, specifially we shall be using :
  1. Binary OR(|) operator (our bucket for storing permissions)
  2. Binary AND(&) operator (our tester, to tell us what if an item is in the bucket)

The binary (|) operator

 If either bit stacked ontop of one another are 1 or both are 1, then the result is 1, otherwise the result is 0. So 1 on 1 = 1, 1 on 0 = 1 but 0 on 0 = 0.

128 64 32 16 8 4 2 1 Calculation Result
0 0 0 0 0 1 1 1 (4+2+1) 7
0 0 0 0 1 0 0 1 (8+1) 9
0 0 0 0 1 1 1 1 (8+4+2+1) 15

As you can see from the above, binary (|) is being performed on each pair of corresponding bits. If either bit stacked ontop of one another are 1 or both are 1, then the result is 1, otherwise the result is 0. So 1 on 1 = 1, 1 on 0 = 1 but 0 on 0 = 0.

In the above example 7 | 9 = 15. How is this useful with our bitmasks ? Not very useful indeed. The result 15 is not making sense as a candidate for our one bit mask but thats because we OR'ed two numbers that are not made of power of 2^ ; Had we used two numbers with the power of 2^, we could use the binary OR(|) operator to add masks.

Now, lets see what happens when we use numbers made up of power of 2^ like below 1 | 8;

128 64 32 16 8 4 2 1 Calculation Result
0 0 0 0 0 0 0 1 (1) 1
0 0 0 0 1 0 0 0 (1) 8
0 0 0 0 1 0 0 1 (8+1) 9

As you can note from the above, 1 | 8 is giving us 9. It's a simple addition, 0001 | 1000 = 1001. So in short, when we OR'd, we can actually think of this simply asif we were adding items to a bucket. In our case we added 1,8 to the bucket. we can add more items. Eg : 1|4|8|16...

128 64 32 16 8 4 2 1 Calculation Result
0 0 0 0 0 0 0 1 (1) 1
0 0 0 0 0 1 0 0 (4) 4
0 0 0 0 1 0 0 0 (8) 8
0 0 0 1 0 0 0 0 (16) 16
0 0 0 1 1 1 0 1 (16+8+4+1) 29

See, we just got a result of 11101(16+8+4+1) ; Our result 11101 contains all 4 items added(or OR'd) to our bucket. In a real application, such as the bitmask we are writing, this can be used to add 1 or more permissions. First lets create a simple permissions table in our database mapping users to permissions, a single int field is all we need.

UserID Permission
1 3
2 5
3 7

The remaining of the job can be done using bitmasks in our front end. But first, why are one bit masks composed of integers to the power of 2 ? Remember that when OR'ing, "If either bit stacked ontop of one another are 1 or both are 1, then the result is 1, otherwise 0". Well, that's pretty much why. Basically, when using integers to the power of 2, we get a sequential shift, look at the table below :

128 64 32 16 8 4 2 1 Calculation Result
0 0 0 0 0 0 0 1 (1) 1
0 0 0 0 0 0 1 0 (2) 2
0 0 0 0 0 1 0 0 (4) 4
0 0 0 0 1 0 0 0 (8) 8
0 0 0 1 0 0 0 0 (16) 16
0 0 1 0 0 0 0 0 (32) 32
0 1 0 0 0 0 0 0 (64) 64
1 0 0 0 0 0 0 0 (128) 128

In the table above, follow the 1'ones hilighted in red. As you can see, the 1's are in sequential shift order. Thanks to this pattern, we never end up with 1's stacked ontop of one another and our items get added, so if we OR'd integer 1 | 8 which are both to the power 2, we end up with a beautiful 9(1+8), while had we used integers that were'nt to the power of 2, eg 7 | 9 which gives us 15(8+4+2+1) <-- not exactly the bucket type functionality we want.(we get one's stacked ontop of one another and lose the bucket type functionality we are seeking). This is why, one bit masks are always composed of integers to the power of 2.

So, next, let's create a class that describes the available permissions we plan to have via public static fields in our AccessControl (static) class. It makes sense to make this a non instance class, since it's common to all of our users.
public static class AccessControl { // 0001 public static int View = 1; // 0010 public static int Post = 2; //0100 public static int Reply = 4; // 0001 public static int ViewOnly = View; // 0001 | 0010 = 0011(1+2=3) public static int ViewAndPost = View | Post; // 0001 | 0100 = 0101(4+1=5) public static int ViewAndReply = View | Reply; // 0001 | 0010 | 0100 = 0111(4+2+1=7) public static int ViewAndPostAndReply = View | Post | Reply; }

Note above how we made use of the binary (|) OR operator ? As you can see, ViewOnly = 0001(1), but the other 3 permissions are OR'd, ViewAndPost = 0011(3), ViewAndReply = 0101(5), ViewAndPostAndReply = 0111(7).

Now, how do we check the bucket ? ViewAndPost contains two permissions assigned to it, View+Post, and ViewAndReply have two permissions assigned to it too, while ViewAndPostAndReply has 3 permissions assigned to it. Checking this can be easily done using the binary AND(&) Operator.

The binary (&) operator

As we have seen in my previous post, this operator is being performed on each pair of corresponding bits. The comparision is made based on both bits stacked ontop of one another. if both are one's, then we have a 1, anything else is a 0. so 1 on 1 = 1, 1 on 0 = 0, 0 on 0 = 0.

128 64 32 16 8 4 2 1 Calculation Result
0 0 0 0 0 1 1 1 (7) 7
0 0 0 0 1 0 0 1 (9) 9
0 0 0 0 0 0 0 1 (1) 1

In the above example 7 & 9 = 1. How can we use this in our bitmasks for checking permissions ? Lets watch another example, except this time lets binary AND(&) the result of a binary OR(|). So, we already know that 1|4|8|16 has given us the result (16+8+4+1), which is 29. Recall also that we called this a bucket of items, and that in this bucket we have the items 16, 8, 4 and 1. Now lets check this bucket for the item 8  by binary AND'ing 29 & 8 ?

128 64 32 16 8 4 2 1 Calculation Result
0 0 0 1 1 1 0 1 (16+8+4+1) 29
0 0 0 0 1 0 0 0 (8) 8
0 0 0 0 1 0 0 0 (8) 8

As you can see from the above table, 29 & 8 = 8. The return value of 8 indicates that 8 is indeed in our bucket. This means we can use the binary (&) operator to check our bucket for items. A simple code example below :
if ((AccessControl.ViewAndPostAndReply &  AccessControl.View) == AccessControl.View) // View is in the bucket else // blah

In a real scenario, we'd have done the following though. Firstly, note that all we need is a single (permission) property added in our User class as per our example below or anywhere else you'd want to map a permission :
public class MyUser { public MyUser() { } private int permissionBucketValue; public int PermissionBucket {   get { return permissionBucketValue; }   set { permissionBucketValue = value; } } }

Then we need to fill it with the value from our database. Designing your db or retrieving a single value from your db is out of the scope of this post, however it's a nobrainer. So get the value and pass it to Permission property in your user class or where ever you need permissions. In your db, all you need is a single int field to hold the OR'd result. So if you planned on giving read access you would be storing the value of AccessControl.View, which is 1 in  your db. If you had planned to give the user ViewAndPost permission, then you will be storing the value of AccessControl.ViewAndPost, which is 3 in your db and so forth. This is just perfect.

Following is the static AccessControl class example we used earlier, with the addition of a new static method HasAccess. If you look at the HasAccess method a bit more closely you can see that the binary AND(&) operator is being used to test against a given permission :

public static class AccessControl { // 0001 public static int View = 1; // 0010 public static int Post = 2; //0100 public static int Reply = 4; // 0001 public static int ViewOnly = View; // 0001 | 0010 = 0011(1+2=3) public static int ViewAndPost = View | Post; // 0001 | 0100 = 0101(4+1=5) public static int ViewAndReply = View | Reply; // 0001 | 0010 | 0100 = 0111(4+2+1=7) public static int ViewAndPostAndReply = View | Post | Reply; public static bool HasAccess(int bucket, Permission p) {   switch (p)   {     case Permission.View:       return ((bucket & View) == View);     case Permission.Post:       return ((bucket & Post) == Post);     case Permission.Reply:        return ((bucket & Reply) == Reply);   }   return false; } }

And our Permissions enum. We want very readable code, so here it is :
public enum Permission { View, Post, Reply }

See, how we check access permissions using the binary AND(&) operator in the static HasAccess method above. It's that simple. We just need to know if the permission we are checking against is valid for the user. So later in our code we can call HasAccess like this :
MyUser u = new MyUser(); //retrieve permissions from db and pass them to u.Permission // later check access permission in your code anywhere eg : if (AccessControl.HasAccess(u.Permission, Permission.Post)) // user has access else // user does not have acess

So really, what's wrong with using normal boolean flags and storing permissions in individual bit fields in your database Versus using bitmasks. I mean whats the big deal really ? Well, here are my favourite 3 issues with that :
  1. You end up creating an individual field in your database for each access permission you want. In this code sample here i have just 3 access permissions(to simplify). You could have 10, 20 and many more fields..

  2. Building onto problem 1 mentioned above, it's not easy to make a change. Say tomorrow you needed to add a new permission, then you end up adding a new field in your database ? and preparing your frontend for this new change.

  3. Your access control in your database is quite legible and easy to understand, since you just used individual fields, that are basically true or false fields for each permission set, so not the best security. Had you used one bit masks, then you'd have been storing only the OR'd result in a single field(which basically means nothing to who ever is reading your database table). Even though, i'll agree that if your db has been compromised, then knowing someone is able to view and edit your access control table is going to be the last of your worries.
Using one bit masks is really quite simple and neat and readable to me. Normally people prefer to use plenty of boolean fields, which is not a big deal and not that much of a memory consumption in a non systems level application. However, using bitmasks as i have shown here is much  much easier, very very readable, and less code. Less code in your front end, less code in your database, since all you need is a single int field to keep track of the permission set. And you save precious memory and time.

Using bitmasks, you can avoid all the above issues and with style. It's up to you :-)

Bitwise operators in c# OR(|), XOR(^), AND(&amp;), NOT(~)

This post is a brush up on some of the bitwise operators that we shall be using for creating bitmasks to set permissions(my next post will be on exactly this subject. Bitmasks).

The c# bitwise operators I shall be covering here and are of particular interest to me in my followup post are :
  1. binary OR(|) operator
  2. binary AND(&) operator
  3. XOR (^) operator
  4. Not (~) operator

Binary OR(|) operator

Binary | operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.
Lets focus on the integral types(types that can be represented as numbers) and how the computation takes place.
protected void Page_Load(object sender, EventArgs e) { byte a = 7; byte b = 9; int orComputed = a | b; Response.Write(string.Format( "<br />{0} | {1} OR computed :{2}"     a, b, orComputed)); }

Output :
7 | 9 Result :15

To understand what just happened we need to look at bits and how each bit(which contains an integral type) is understood by the computer. The computer is going to understand this as binary data in the form of 0's and 1's eg. -->> 00101010(which represents the number 42), this is actaully much easier for a computer to handle since each bit(or integral type as in number), can be interpreted as either on or off signal.

0 = off
1 = on

To understand the bitwise operator with clarity and how it computes the result, lets first construct a base 2 table from right to left incrementing the exponent by 1 for each power. So, since we have 8 bits in a byte, we make a table that lists 8 elements 128, 64, 32, 16, 8, 4, 2, 1.

128 64 32 16 8 4 2 1 Result
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 0 2
0 0 0 0 0 1 0 0 4
0 0 0 0 1 0 0 0 8
0 0 0 1 0 0 0 0 16
0 0 1 0 0 0 0 0 32
0 1 0 0 0 0 0 0 64
1 0 0 0 0 0 0 0 128

If you will look at the above pattern more closely, we notice how the 1 keeps moving 1 digit to the left for every new value in binary. This is also known as a "bit shift".

Ok, now that we have our binary 8bit table with digits to the power of 2^ up to 128, so for a simple 7 | 9, which yielded the result 15, lets look at how it was computed by first creating the binary representation using our base 2 table and then computing the binary | operation, so :

128 64 32 16 8 4 2 1 Calculation Result
0 0 0 0 0 1 1 1 (4+2+1) 7
0 0 0 0 1 0 0 1 (8+1) 9
0 0 0 0 1 1 1 1 (8+4+2+1) 15

As you can see from the above, binary (|) is being performed on each pair of corresponding bits. If a 1 is present in the either bit or in both bits, otherwise it's 0. Not clear ? Lets try that again. How did we get the result 1111 ? It's a simple comparision. If either bit stacked ontop of one another are 1 or both are 1, then the result is 1, otherwise the result is 0. So 1 on 1 = 1, 1 on 0 = 1 but 0 on 0 = 0.

Binary AND(&) operator

Quoting MSDN Docs :
Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.
So, lets quickly test this :

protected void Page_Load(object sender, EventArgs e) { byte a = 7; byte b = 9; int orComputed = a & b; Response.Write(string.Format( "<br />{0} & {1} Result :{2}"     a, b, orComputed)); }

Output is :
7 & 9 Result :1

Lets convert to binary using our base 2 table, and dig deeper  :

128 64 32 16 8 4 2 1 Calculation Result
0 0 0 0 0 1 1 1 (4+2+1) 7
0 0 0 0 1 0 0 1 (8+1) 9
0 0 0 0 0 0 0 1 (1) 1

so, how did we get the binary result 0001(1) ?  binary (&) is being performed on each pair of corresponding bits. The comparision is made based on both bits stacked ontop of one another. if both are one's, then we have a 1, anything else is a 0. so 1 on 1 = 1, 1 on 0 = 0, 0 on 0 = 0.
Binary Xor (^) operator

Quoting MSDN Docs:
Binary ^ operators are predefined for the integral types and bool. For integral types, ^ computes the bitwise exclusive-OR of its operands. For bool operands, ^ computes the logical exclusive-or of its operands; that is, the result is true if and only if exactly one of its operands is true.
A simple c# example :

protected void Page_Load(object sender, EventArgs e) { sbyte a = 7; sbyte b = 9; int orComputed = a^b;//on a negative Response.Write(string.Format( "<br />{0} ^ {1} Result :{2}"     a,b, orComputed.ToString())); }
output :
7 ^ 9 Result :14

Now, how did binary (^) Xor'ing 7 ^ 9 produce 14 ? This is because binary (^) is being performed on each pair of corresponding bits. If we have two matching zero's or one's, then the result is 0, otherwise 1. SO, 1 on 1 = 0, 0 on 0 = 0, 1 on 0 = 1, 0 on 1 = 1. Lets look at an example in our base 2 table of 7 and 9 in binary format :

128 64 32 16 8 4 2 1 Calculation Result
0 0 0 0 0 1 1 1 (4+2+1) 7
0 0 0 0 1 0 0 1 (8+1) 9
0 0 0 0 1 1 1 0 (8+4+2) 14

Not (~) operator

Quoting MSDN Docs:
The ~ operator performs a bitwise complement operation on its operand, which has the effect of reversing each bit. Bitwise complement operators are predefined for int, uint, long, and ulong.
This is also a unary operator so we don't need to pass a value. Unary operators perform an operation on a single operand eg. ~n and not x~y.
Binary (~) operator is the most confusing one. It performs reversing of each bit, so reversing a positive can produce a negative value. Negative Values Use Two's Complement format and is quite tricky.
A simple example :

protected void Page_Load(object sender, EventArgs e) { sbyte a = 7; int orComputed1 = ~-a;//on a negative int orComputed2 = ~a;// on a positive Response.Write(string.Format( "<br />~-{0} Result :{1}"       a, orComputed1)); Response.Write(string.Format( "<br />~+{0} Result :{1}"       a, orComputed2)); }
output :
~-7 Result :6
~+7 Result :-8

As you can see, when the NOT(~) operator is applied to a positive number, the resulting value is a negative. This is because the value is reversed. Since this reversing yields a negative or positive depending on what value your reversing, use a signed type (a type that can store a negative value).

In the base table below, our binary number gets inverted. all 1's become 0 and 0 becomes 1.

A signed binary uses the left most bit to keep track of the sign(negative or positive).
In the c# compiler, negative values are represented internally in two's complement format. Two's complement can be obtained by negating each bit of the value, then adding 1. Performing two's complement twice generates the original value.

In the following base 2 table, one typical gotcha when inverting is to remember to start the inversion from the left of the first 1 value in our binary. So, say had we a binary representation of the digit 7(00000111), then we start inverting at the 2nd digit starting at the right, skipping the first(since it's 1)  and get 11111001. A last thing to also note is how in our calculation we added a negative -1 to our result instead of a positive 1 -->> (-128+64+32+16+8+1)-1 ; While the two's complement format states that we need add 1, we are actually deducting 1 versus adding 1. This is because we have to consider 0 in the equation.

An easy method to get the two's complement of +7 :(left most is used to track sign, so add -1 to the result) :

128 64 32 16 8 4 2 1 Calculation Result
0 0 0 0 0 1 1 1 (4+2+1) 7
1 1 1 1 1 0 0 1 (-128+64+32+16+8+1)-1 -8

Note the left most significant pair of corresponding bits in red. A value of 0 means positive and a value of 1 means negative and is used for tracking the sign in a signed binary.

One question still remains, why is the two's complement always a positive less(6) and a negative more(-8) ?
Why aren't we getting -7 for a 7 and vice versa. I guess the answer to this is simply "logic", we have to consider the 0.

-8 -7 -6 -5 -4 -3 -2 -1
7 6 5 4 3 2 1 0