Template class to manipulate bits of undefined type | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 1, 2002
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.