Template class to manipulate bits of undefined type

This template class is for manipulation bits of undefined type. It’s
usually for unsigned type but you can use it with signed type.

Most of the methods are inline for speed.

The method “print”, in case of decimal value, formats only unsigned
value. With signed type, the signe is lost (formally not lost but it
won’t be printed).

I didn’t try it with floating number, I think it will give unexpected

I made a zip file where the CBit class ( cpp & h ) is ready to compile
in a project.

If you have any remarks or any improvment to do or if this class works
fine send me a mail

On a suggestion of Dave Montgomery <[email protected]> from Canada, I
integrate an optimization in inline method SetBit0 and SetBit1 and add a
new method SetBit.

In the header file
// I didn't find useful to derive this class from CObject because in this
// case Run-time class information, Dynamic creation, Persistence
// (serialization), Run-time object diagnostics is heavy where the initial
// design goal was lightness, speed, speed and only speed.
template<class T> class CBit
    T m_TVar;

        CBit(T l_TVar) { m_TVar = (T)l_TVar;}
        ~CBit() {};

        T Get() { return m_TVar;}; //Get the value
        T Set(T l_TVar) { m_TVar = (T)l_TVar;};//Set a new value

        int GetMaxBits(void) {return (sizeof(T)*8); }; //give the maximum number of bits of the type

    enum BitFormat

    CString print (CBit<T>::BitFormat l_eBitFormat); //prints the value in various way, have a look on file bit.cpp 

    //rank is zero based working with bit
    void SetBit1(unsigned int nRang) {
        ASSERT(nRang < (sizeof(T)*8)); //rank overflow
		T mask = 1; m_TVar |= (mask << nRang);

    void SetBit0(unsigned int nRang) {
        ASSERT(nRang < (sizeof(T)*8)); //rank overflow
		T mask = 1; m_TVar &= ~(mask << nRang);

	void SetBit(unsigned int nRang, unsigned int val) {
		(val) ? SetBit1 (nRang) : SetBit0 (nRang);

    int GetBit(unsigned int nRang){
        ASSERT(nRang < (sizeof(T)*8));//rank overflow
        return ( (m_TVar >> nRang) & 0x1);

Example of using
void yourFunction ()
     CBit< unsigned int > var(0x330);
     TRACE(_T("Variable %sn"),var.print(CBit<unsigned int>::All));

     TRACE(_T("Variable %sn"),var.print(CBit<unsigned int>::All));
     int value0 = var.GetBit(0);
     int value4 = var.GetBit(4);
     int value5 = var.GetBit(5);
     int value6 = var.GetBit(6);
     int value7 = var.GetBit(7);
     int value8 = var.GetBit(8);
     int value9 = var.GetBit(9);
     int value31 = var.GetBit(31);
     TRACE(_T("value0=%d value4=%d; value5=%d value6=%d; value7=%d
               value8=%d; value9=%d value31=%dn"),value0 ,value4, value5,value6,
               value7,value8, value9,value31 );
} //end of yourFunction ()

Result in TRACE Window

Variable 0000 0000 0000 0000 0000 0011 0011 0000 0x330 816 01460
Variable 1000 0000 0000 0000 0000 0001 0010 0001 0x80000121 2147483937
value0=1 value4=0; value5=1 value6=0; value7=0 value8=1; value9=0

End of Result in TRACE Window

Download source – 3KB

Date Posted: 5/4/98

Last Updated: 5/12/98

Posted by: Pat Laplante.

More by Author

Must Read