Search code examples
maskcpu-architecturenibble

The Idea of Mask


enter image description here

I don't understand the idea of mask and why it is useful here?


Solution

  • Supposed you have a value and you want to set all bits to zero except for a few that you're interested in. So you create a mask which has only 1s in the positions you want to keep protected. When you use an AND operation with your mask that's the result you get. It appears to me that $t1 is the value being used above as the mask.

    You might be familiar with how a painter uses masking tape to cover an area that shouldn't be painted. It's the same kind idea.

    There are basically three different operations where you'll mask bits this way: AND to clear bits, OR to set bits and XOR to toggle bits.