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

  • This ESG study by Mark Peters evaluated a common industry-standard disk VTl deduplication system (with 15:1 reduction ratio) versus a tape library with LTO-5, drives with full nightly backups, over a five-year period.  The scenarios included replicated systems and offsite tape vaults.  In all circumstances, the TCO for VTL with deduplication ranged from about 2 to 4 times more expensive than the LTO-5 tape library TCO. The paper shares recent ESG research and lots more. 

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds