Click here to Skip to main content
14,035,995 members
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 1 Feb 2006

C# Bitwise Helper Class

, 1 Feb 2006
Rate this:
Please Sign up or sign in to vote.
A class that helps with some basic bitwise operations.


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):

///Gets the size of int in bits.
public static int SizeOf ( int pInput );
///Gets the bits at the specified location
public static int GetBits ( int pInput, int pStartIndex, 
                                          int pLength );
///Sets the bits at the specified location
public static int SetBits ( int pDest, int pSource, 
       int pSourceIndex, int pDestIndex, int pLength );
///Checks to see if the bit is set at the specified position
public static bool IsBitSet ( int pInput, int pPosition);
///Xor the value of the bit at the specified location
public static int ChangeBit ( int pInput, int pPosition );
///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


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


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralBit Manipulation and SQL Compliancy Pin
Tim McCurdy7-Feb-06 4:17
memberTim McCurdy7-Feb-06 4:17 
GeneralMissing ! Pin
ziade3-Feb-06 12:45
memberziade3-Feb-06 12:45 
there is a mising ! in the 2nd part of the if statement on all the changebit methods.

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190424.1 | Last Updated 2 Feb 2006
Article Copyright 2006 by ziade
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid