|
Used to calculate XOR bitmask
Wikipedia:
https://en.m.wikipedia.org/wiki/Mask_(computing)
Toggling bit values
Sometimes it does not really matter what the value is, but it must be made the opposite of what it currently is. This can be achieved using the XOR (exclusive or) operation. XOR returns 1 if and only if an odd number of bits are 1. Therefore, if two corresponding bits are 1, the result will be a 0, but if only one of them is 1, the result will be 1. Therefore inversion of the values of bits is done by XORing them with a 1. If the original bit was 1, it returns 1 XOR 1 = 0. If the original bit was 0 it returns 0 XOR 1 = 1. Also note that XOR masking is bit-safe, meaning that it will not affect unmasked bits because Y XOR 0 = Y, just like an OR.
Example: Toggling bit values
10011101 10010101
XOR 00001111 11 11 11 11
-----------------
= 10010010 01101010
|