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

  • You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this webcast that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base application that will jumpstart you into building your own more complex app …

  • Not all enterprise applications are created equal. Sophisticated applications need developer support but other more basic apps do not. With the right tools, everyone is a potential app developer with ideas and a perspective to share. Trends such as low-code development and model driven development are fundamentally changing how and who creates applications. Is your organization ready? Read this report and learn: The seven personas of enterprise app delivery How application ownership is spreading to the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds