5,696,576 members and growing! (17,686 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

C# Bitwise Helper Class

By ziade

A class that helps with some basic bitwise operations.
C#, .NET, WinXP, Windows, Visual Studio, Dev

Posted: 1 Feb 2006
Updated: 1 Feb 2006
Views: 24,043
Bookmarked: 20 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
13 votes for this Article.
Popularity: 3.99 Rating: 3.59 out of 5
0 votes, 0.0%
1
2 votes, 15.4%
2
2 votes, 15.4%
3
3 votes, 23.1%
4
6 votes, 46.2%
5

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

///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 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

About the Author

ziade



Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralBit Manipulation and SQL CompliancymemberTim McCurdy5:17 7 Feb '06  
GeneralMissing !memberziade13:45 3 Feb '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Feb 2006
Editor: Smitha Vijayan
Copyright 2006 by ziade
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project