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

  • Webinar on September 23, 2014, 2 p.m. ET / 11 a.m. PT Mobile commerce presents an array of opportunities for any business -- from connecting with your customers through mobile apps to enriching operations with mobile enterprise solutions. Join guest speaker, Michael Facemire, Forrester Research, Inc. Principal Analyst, as he discusses the new demands of mobile engagement and how application program interfaces (APIs) play a crucial role. Check out this upcoming webinar to learn about the new set of …

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds