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.


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;

    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;
    *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);



  • Excellant.... u did a work!

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

    Originally posted by: Rao Hammad Aslam

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

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

Top White Papers and Webcasts

  • This paper introduces IBM Java on the IBM PowerLinux 7R2 server and describes IBM's implementation of the Java platform, which includes IBM's Java Virtual Machine and development toolkit.

  • In the competitive marketplace that surrounds us today, customers shouldn't have to settle for legacy desktop or application delivery simply because they've relied on a certain vendor in the past. This white paper reviews how three customers decided to partner with VMware, and how they benefited from the latest VDI and app trends to improve the end-user experience, increase productivity, reliability and stability to deliver better SLAs - with lower cost and less time needed to manage end users.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds