#111 – Combining the Logical or Shift Operators with the Assignment Operator

To perform a logical bitwise operation on a variable and assign the result back to the original variable, you can use the following shortcut.

Instead of writing :

 x = x & 0x0020;

You can write :

 x &= 0x0020;

You can do the same thing with the other bitwise logical operators and the shift operators:

 x |= 0x2200;   // Logical OR
 x ^= 0x1A1A;   // Logical XOR
 x <<= 4;       // Left-shift x by 4 bits
 x >>= 3;       // Right-shift x by 3 bits
Advertisements