http://www.somacon.com/p125.php
Toggling a bit and leaving all other bits unchanged
x = x ^ mask;
(or shorthand x ^= mask;)
Bits that are set to 1 in the mask will be toggled in x.
Bits that are set to 0 in the mask will be unchanged in x.
Toggling means that if the bit is 1, it is set to 0, and if the bit is 0, it is set to 1.
XOR truth table
0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0
Setting a bit to zero and leaving all other bits unchanged
x = x & (~mask);
(or x &= (~mask);)
Bits that are set to 1 in the mask will be set to zero in x.
Bits that are set to 0 in the mask will be unchanged in x.
AND truth table
0 & 0 = 0
1 & 0 = 0
0 & 1 = 0
1 & 1 = 1
Setting a bit to one and leaving all other bits unchanged
x = x | mask;
(or x |= mask;)
Bits that are set to 1 in the mask will be set to one in x.
Bits that are set to 0 in the mask will be unchanged in x.
OR truth table
0 | 0 = 0
1 | 0 = 1
0 | 1 = 1
1 | 1 = 1