It's common to perform a check and set/clear a flag, eg:
if (some_test) {
flag |= SOME_FLAG;
}
else {
flag &= ~SOME_FLAG;
}
A convenient way around this I found so far is...
flag = (some_test) ? (flag | SOME_FLAG) : (flag & ~SOME_FLAG);
This could be made into a macro and its OK, but is there some bit-twiddeling magic to avoid referencing flag twice?
(in case multiple instantiations of flag
causes overhead).
Example of what I'm looking for (if C could do ternary operations on operators), is...
flag ((some_test) ? (|=) : (&= ~) SOME_FLAG;
The example above is only to describe what I'm looking for, of course it wont work in its current form.