Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#
Article

C# Bitwise Helper Class

Rate me:
Please Sign up or sign in to vote.
3.76/5 (16 votes)
1 Feb 20061 min read 77.8K   1.7K   30   2
A class that helps with some basic bitwise operations.

Introduction

I have been recently working on a project that requires a lot of low level programming. Reading and writing not of just bytes but the bits in those bytes as well. The .NET framework provides bitwise operators for dealing with values less than a byte. Unfortunately, the .NET framework does not have any easy way of doing some of the things we are going to be doing repeatedly in our project while dealing with bits (at least as far as I can tell) (besides, the BitArray class and the BitArray class don't provide out of the box functionality of some of the things). If there are things in the framework that already do this stuff, please send me an e-mail. So we have created a class called the BitHelper to help us with some of our basic operations dealing with bits.

The class we created contains methods for some of the primitive types (unsigned byte, short, int, long). These are all I needed for the project. Maybe later I can add overloads for all primitives. Or maybe someone can do it for me and e-mail me the code. There are six basic methods for each primitive type, with several overrides. The methods provided are (using the int data type and I did not include the overrides):

C#
///Gets the size of int in bits.
public static int SizeOf ( int pInput );
C#
///Gets the bits at the specified location
public static int GetBits ( int pInput, int pStartIndex, 
                                          int pLength );
C#
///Sets the bits at the specified location
public static int SetBits ( int pDest, int pSource, 
       int pSourceIndex, int pDestIndex, int pLength );
C#
///Checks to see if the bit is set at the specified position
public static bool IsBitSet ( int pInput, int pPosition);
C#
///Xor the value of the bit at the specified location
public static int ChangeBit ( int pInput, int pPosition );
C#
///Set the value of the bit at the specified location
public static int SetBit ( int pInput, int pPosition, bool pOn );

There are also some methods that perform the same tasks as some of the Windows macros: HiByte, LoByte, HiWord, LoWord, MakeWord, etc.

There is a test console app that calls some of these methods and prints the output.

If anyone finds any bugs, has any improvements, knows of better ways to do this stuff, or has any feedback at all, please send me an e-mail at ziadelmalki@hotmail.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralBit Manipulation and SQL Compliancy Pin
Tim McCurdy7-Feb-06 4:17
Tim McCurdy7-Feb-06 4:17 
GeneralMissing ! Pin
ziade3-Feb-06 12:45
ziade3-Feb-06 12:45 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.