Definition: Masking is the process of changing bits in a byte.
We start with a Value 01010011 which we wish to combine with Data 1DD10DD1. We only wish to alter the bits in the Data that are not 'D's. These could be 0 or 1s, but that is not important- we just wish to leave them unchanged. We'll use masking to do this.
- First we generate a mask from the Data. This has a 0 in every bit position we can change in the Data, a 1 in every bit position we do not want to change. So this is 01100110. We AND (&) this with the Data to clear every bit that can be changed and leave the rest intact. 0DD00DD0.
- Next we generate the invert of the Mask which is 10011001 and we AND (&) this with the Value (01010011) with this to produce 00010001.
- Finally we OR (|) this (00010001) with the masked data (0DD00DD0) to give the final value 0DD10DD1.
01010011 - Value
1DD10DD1 - Data
01100110 - Mask (From Data)
0DD00DD0 - (Mask & Data)
100111001 - Inverted Mask
00010001 - (Inverted Mask & Value)
--------------------
0DD10DD1 - (Inverted Mask & Value) | (Mask & Data)

