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,
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):
public static int SizeOf ( int pInput );
public static int GetBits ( int pInput, int pStartIndex,
int pLength );
public static int SetBits ( int pDest, int pSource,
int pSourceIndex, int pDestIndex, int pLength );
public static bool IsBitSet ( int pInput, int pPosition);
public static int ChangeBit ( int pInput, int pPosition );
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:
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 firstname.lastname@example.org.