I came across this handy shortcut earlier today.  It is the AND assignment operator and works just like +=.  I’ve used += in various C like languages for years but it never occurred to me that the same syntax could be used with other assignment types.

For example if we want to test a particular bit we might write

x = x & y;
when in actual fact we can save ourselves a keystroke and write
x &= y;

This makes the syntax for flipping a bit in an enum as simple as:

myEnum.bits &= ~someBit;

Advertisements