<span style="font-size:18px;">// bit_opt.cpp : 定义控制台应用程序的入口点。
//
/************************************************************************/
/*
author:郑金玮
time:2014/07/14
desc:impl bit set and clear test demo
*/
/************************************************************************/
#include "stdafx.h"
#include <process.h>
#include <iostream>
using namespace std;
#define bit_move_left(bit) (1<<(bit) )
class CBitOpt
{
public:
CBitOpt(){
cout<<"construct function called."<<endl<<endl;
}
~CBitOpt(){
cout<<"destructrut function called."<<endl<<endl;
}
public:
enum eBitTag{
bit_share=bit_move_left(1),
bit_private=bit_move_left(31),
bit_count=2
};
void setbit(eBitTag bit){ m_nBit = m_nBit | bit; }
void clearbit(eBitTag bit) { m_nBit = m_nBit & ~bit ;}
bool checkbit(eBitTag bit) { return m_nBit & bit ;}
void clear() { m_nBit = 0x0000;}
private:
int m_nBit;
};
int _tmain(int argc, _TCHAR* argv[])
{
CBitOpt bitoptobj;
bitoptobj.setbit(CBitOpt::bit_private);
bitoptobj.setbit(CBitOpt::bit_share);
if (bitoptobj.checkbit(CBitOpt::bit_private))
{
cout<<"this tag has private bit"<<endl;
}
else
{
cout<<"this tag has not private bit"<<endl;
}
if (bitoptobj.checkbit(CBitOpt::bit_share))
{
cout<<"this tag has share bit "<<endl;
}
else
{
cout<<"this tag has not share bit "<<endl;
}
cout<<endl<<endl<<endl<<"==============after clear bit==========="<<endl<<endl<<endl;
bitoptobj.clearbit(CBitOpt::bit_private);
if (bitoptobj.checkbit(CBitOpt::bit_private))
{
cout<<"this tag has private bit"<<endl;
}
else
{
cout<<"this tag has not private bit"<<endl;
}
if (bitoptobj.checkbit(CBitOpt::bit_share))
{
cout<<"this tag has share bit "<<endl;
}
else
{
cout<<"this tag has not share bit "<<endl;
}
cout<<endl<<endl<<endl<<"--------------after clear all bits----------"<<endl<<endl<<endl;
bitoptobj.clear();
if (bitoptobj.checkbit(CBitOpt::bit_private))
{
cout<<"this tag has private bit"<<endl;
}
else
{
cout<<"this tag has not private bit"<<endl;
}
if (bitoptobj.checkbit(CBitOpt::bit_share))
{
cout<<"this tag has share bit "<<endl;
}
else
{
cout<<"this tag has not share bit "<<endl;
}
system("pause");
return 0;
}</span>