I have bitwise enum for save a two state settings of some class. I try to develope easy function to switch the bit ON or OFF. Is this proper and quick way to do it?
enum myEnum { eeNull = 0, ee1 = 1, ee2 = 2, ee4 = 4, ee8 = 8 };
template<class T>
inline T &SetBit(T &eValue, const int iBit, const bool bState)
{
return eValue = (T) ((eValue| (iBit * bState)) & ~(iBit * !bState));
}
int main()
{
myEnum eE;
eE = eeNull;
SetBit<myEnum>(eE, ee2, true);
SetBit<myEnum>(eE, ee1, true);
SetBit<myEnum>(eE, ee2, false);
return 0;
}
This is how you would do it typically: