CBitPointer: Easy Bit Manipulation

I wrote a program that uses compression algorithms. These algorithms require a heavy bit manipulation. To make life easier, I wrote two classes: CBitPointer and its supporting class CBit. These two classes made a bit (nearly) look and feel like any simple data type (for example, char, int, and so forth). Now, by using these classes, you don't have to worry about simple data objects boundaries when you iterate to read/write a variable length of bit string.

The CBitPointer object points to a single bit in memory and can be used as any pointer. You can use:

  • A pointer dereference to get a CBit object.
  • A pointer arithmetic (increment, decrement, difference, adding integers, and subtracting integers).
  • An array reference to get a CBit object.

Examples

For example, to count 1s in a string using array reference, you would use this code example:
size_t bitcnt(void* p_pvString, size_t p_cBits)
{
  CBitPointer pbit;
  size_t cCount;
  size_t i;

  pbit = p_pvString;
  cCount = 0;

  for(i = 0; i < p_cBits; i++)
    cCount += pbit[i];

  return cCount;
}

int main()
{
  . . .
  char szString[] = "Number of 1's in a string.";
  size_t cCount;
  cCount = bitcnt(szString, strlen(szString)*8);
  . . .
}

A more efficient way to write a bitcnt function is to iterate through the string using the pointer, as shown here:

size_t bitcnt(void* p_pvString, size_t p_sizBits)
{
  CBitPointer pbit;
  size_t cCount;

  pbit = p_pvString;
  cCount = 0;

  while(p_sizBits--)
    cCount += *pbit++;

  return cCount;
}

Another example could be to copy the bit string and to move the pointer with the size of the bit string. Consider this example:

CBitPointer bitcpy(CBitPointer p_pbitDestination,
                   CBitPointer p_pbitSource, 
                   size_t p_cSize)
{
  CBitPointer pbitTemp;

  pbitTemp = p_pbitDestination;
  while(p_cSize--)
    *p_pbitDestination++ = *p_pbitSource++;

  return pbitTemp;
}

int main()
{
  . . .
  bitcpy(pbitOutput, Table[i].pbitCode, Table[i].cLength);
  pbitOutput += Table[i].cLength;
  . . .
}

How to Integrate It into Your Code

Add the Bit.h, Bit.cpp, BitPointer.h, and BitPointer.cpp files to your project and put the following line in your header/source file(s):

#include "BitPointer.h"

If you don't use a pre-compiled header in your project, remove the following line from the Bit.cpp and BitPointer.cpp files:

#include "stdafx.h"

Code Update Log

I noticed that the last uploaded source code is very old version, so I uploaded my latest version.

  • Latest version is more tested and very reliable
  • It is optimized for runtime speed
  • I included some of my bit pointer utility functions. Here is copy & paste from BitLib.h header file:
//copy bit strings from source to destination with bit count length
CBitPointer bitcpy(CBitPointer p_pbitDestination, CBitPointer p_pbitSource, size_t p_cLength);

//compare two bit strings
// result = -1 => less than
// result =  0 => equal
// result = +1 => greater than
int bitcmp(CBitPointer p_pbitString1, CBitPointer p_pbitString2, size_t p_cLength);

//invert bit string
CBitPointer bitinv(CBitPointer p_bitString, size_t p_cLength);

//reverse bit string order
CBitPointer bitrev(CBitPointer p_pbitString, size_t p_cLength);

//count of bits having value 0 or 1
int bitcnt(CBitPointer p_pbitString, int p_iValue, size_t p_cLength);

//count of bits having value 1
int bitcnt(CBitPointer p_pbitString, size_t p_cLength);


Downloads

Comments

  • Excellant.... u did a work!

    Posted by Legacy on 12/07/2002 12:00am

    Originally posted by: Rao Hammad Aslam

    hi
    
    u talked about bit manipulation
    Why do you consider ur class better than bitwise operators?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds