-
Negative Or
I'm making a flags class in C++ for some practice. I have a function that should reset a flag. I figured it would work by NORing the bit value with the flags variable. I just cant work out how to stick not NOT in there. Heres my non-working code. It should explain better what I'm trying to do here:
Code:
bool Flag32::ResetBit(Uint8 bit)
{
if(bit < 1 || bit > 32) return false;
flags !|= (1 << (bit-1));
return true;
}
Someone know how I can do this? I know I could do this by checking if the bit is on or off, then subtracting or not, I'd just like to be able to Nor stuff :)
-
AND it with the complement of the bit:
~flag has 1's everywhere except at the actual flag, which is zero. So by ANDing this pattern with the value, you keep everything the same except the bit you want to turn off.
EDIT: Also, this is a good opportunity to make this a template. Instead of Flag32, how about:
Code:
template <typename IntType>
class Flag
{
};
Now you can use any type you want as the underlying flags type.
-
Oh yeah I forgot that wavy thing is the binary inverter. Being able to set the field type would be better. I havent used any templates yet, but I guess this would be a good time to start. Cheers.