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 result.

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 <dmont@tradeit.com> 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;

    public:
        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
    {
        Decimal,
        Hexa,
        Octal,
        Binary,
        All
    };

    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 %s\n"),var.print(CBit<unsigned int>::All));
     var.SetBit1(0);
     var.SetBit0(4);
     var.SetBit0(9);
     var.SetBit1(31);

     TRACE(_T("Variable %s\n"),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=%d\n"),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
020000000441
value0=1 value4=0; value5=1 value6=0; value7=0 value8=1; value9=0
value31=1

End of Result in TRACE Window


Download source - 3KB

Date Posted: 5/4/98
Last Updated: 5/12/98
Posted by: Pat Laplante.



Comments

  • www.usadevelopers.com

    Posted by Legacy on 04/06/2002 12:00am

    Originally posted by: Daud Nasir

    How about this code !!!!!!!!!!!!!!!!
    //////////////////////////////////////
    #include <iostream.h>
    #include <conio.h>


    void main(void)
    {
    int i,j,k ;

    clrscr();

    for(i=1 ; i <= 10 ; i++)
    {
    for(j=10; j>=i ; j--)
    {
    cout << " " ;
    }
    for(j=1; j<=i ; j++)
    {
    cout << "*" ;
    }
    for(j=2; j<=i; j++)
    {
    cout <<"*";
    }
    cout << "\n" ;

    }

    for(i=2 ; i <=10 ; i++)
    {
    for(j=1 ; j<=i ; j++)
    {
    cout << " ";
    }
    for(j=10;j>=i ; j--)
    {
    cout << "*";
    }
    for(j=9; j>=i ; j--)
    {
    cout << "*";
    }

    cout << "\n";

    }

    getche();
    }

    Reply
  • new programme

    Posted by Legacy on 01/13/2002 12:00am

    Originally posted by: maiwish

    void main()
    {
    int a,b;
    clrscr();
    printf("enter the three values");
    scanf(%d%d%d,a,b,c);
    getch();
    }

    Reply
  • new programme

    Posted by Legacy on 01/13/2002 12:00am

    Originally posted by: maiwish

    void main()
    {
    int a,b,c;
    clrscr();
    printf("enter the three values");
    scanf(%d%d%d,a,b,c);
    getch();
    }

    Reply
  • how about std support

    Posted by Legacy on 11/09/2001 12:00am

    Originally posted by: yuriy georgadze

    What's wrong with std::bitset<size_t > or even std::vector<bool> ?

    Reply
  • Link error with using print function

    Posted by Legacy on 10/11/2000 12:00am

    Originally posted by: Jon Bao

    I couldn't use the function of print(). I exactly followed the example, and I got error: LNK2001 and LNK1120. Why? How can I fix it?

    Jon

    Reply
  • gfdsgfdsg

    Posted by Legacy on 02/09/2000 12:00am

    Originally posted by: gfdsgf

    gfdsgfdsgfdsg

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

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • The hard facts on SaaS adoption in over 80,000 enterprises: Public vs. private companies Mid-market vs. large enterprise GoogleApps, Office365, Salesforce & more Why security is a growing concern Fill out the form to download the full cloud adoption report.

Most Popular Programming Stories

More for Developers

RSS Feeds