ChangedSw = (OldSw ^ ~NewSw) & ~OldSw;
It means "zero one part of OldSw
and inverse other part". NewSw
indicates what bits of OldSw
to zero and what bits to inverse. Namely, 1's in NewSw
indicate bits to be zeroed, 0's indicate bits to be inverted.
This operation implemented in two steps.
Step 1. Invert bits.
(OldSw ^ ~NewSw)
:
0001 1101
^ 0000 1111
---------
0001 0010
See, we inverted bits which were 0's in original NewSw
.
Step 2. Zero bits which were not inverted in previous step.
& ~OldSw
:
0001 0010
& 1110 0010
---------
0000 0010
See, it doesn't change inverted bits, but zero all the rest.