Click here to Skip to main content
Click here to Skip to main content

Bitwise–some thing you learned during college time.

By , 23 Nov 2012
 

Convert 10112 to base 10.

1 x 20 = 1
1 x 21 = 2
0 x 22 = 0
1 x 23 = 8

1 + 2 + 0 + 8 = 11

Answer:  10112 = 1110

Is this look familiar? I learned this during my college but I don’t understand how can we use it. Until…I code.

static void Main(string[] args)
{
Console.WriteLine((7 | 3).ToString());
Console.WriteLine((7 & 3).ToString());
Console.Read();
}

Try to run this code at Visual Studio, and you will have this.

image

Let’s start the math resolution now.

7 = 1112 or [22 + 21 + 20 = 7] or [4 + 2 + 1 =7]

3 = 112 or [21 + 20 = 3] or [2 + 1 = 3]

image

image

Remembered? logical operator. 1 AND 1 = 1, 1 AND 0 = 0, 1 OR 1 = 1, 1 OR 0 =1…etc.

Let’s start the code now.

image

Let’s say, we have a survey requirement. See how we can store multiple selection in a field by using bitwise.

public enum Favorites
{
WCF = 1,
WPF = 2,
Web = 4,
Silverlight = 8,
None = 0
}

First, we create an Enum class with predefine value for each selection.

<asp:UpdatePanel ID=”UpdatePanel1″ runat="”server”">
<ContentTemplate>
<p>
Please choose your favorite topic[s].
</p>
<asp:CheckBox ID=”WCF” runat="”server”" Text=”WCF” />
<br />
<asp:CheckBox ID=”WPF” runat="”server”" Text=”WPF” />
<br />
<asp:CheckBox ID=”Web” runat="”server”" Text=”Web” />
<br />
<asp:CheckBox ID=”Silverlight” runat="”server”" Text=”Silverlight” />
<p>
<asp:Label ID=”Label2″ runat="”server”" Text=”Value” AssociatedControlID=”DecimalValueText_Get”></asp:Label>
<asp:TextBox ID=”DecimalValueText_Get” runat="”server”"></asp:TextBox>
<asp:Button ID=”GetValue” runat="”server”"
Text=”Get Value” OnClick=”GetValue_Click” />
</p>
<p>
<asp:Label ID=”Label1″ runat="”server”" Text=”Value” AssociatedControlID=”DecimalValueText_Set”></asp:Label>
<asp:TextBox ID=”DecimalValueText_Set” runat="”server”" Text=”0″></asp:TextBox>
<asp:Button ID=”SetValue” runat="”server”"
Text=”Set Value” OnClick=”SetValue_Click” />
</p>
</ContentTemplate>
</asp:UpdatePanel>
private Int32 Retrieve()
{
Int32 result = 0;
result = WCF.Checked ? (result | Favorites.WCF.GetHashCode()) : result;
result = WPF.Checked ? (result | Favorites.WPF.GetHashCode()) : result;
result = Web.Checked ? (result | Favorites.Web.GetHashCode()) : result;
result = Silverlight.Checked ? (result | Favorites.Silverlight.GetHashCode()) : result;
return result;
}

Then, follow by a function to construct a integer value according to the selection.

private void Display()
{
Int32 favorites = int.Parse(DecimalValueText_Set.Text);
WCF.Checked = ((favorites & Favorites.WCF.GetHashCode()) == Favorites.WCF.GetHashCode());
WPF.Checked = ((favorites & Favorites.WPF.GetHashCode()) == Favorites.WPF.GetHashCode());
Web.Checked = ((favorites & Favorites.Web.GetHashCode()) == Favorites.Web.GetHashCode());
Silverlight.Checked = ((favorites & Favorites.Silverlight.GetHashCode()) == Favorites.Silverlight.GetHashCode());
}

To make the solution more complete, we create another function to make the selection according to a defined value.

image

Run the project, now you can select multiple items and click “GetValue” to get the result.

image

Or, key in a result and let the program to make the selection by click on “SetValue” button.

Download Attachment 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Cruz Boon
Team Leader Powercomp Software Sdn Bhd
Malaysia Malaysia
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionFlagsAttribute Classmemberaggelos@next24 Nov '12 - 0:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 23 Nov 2012
Article Copyright 2012 by Cruz Boon
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid