Skip to main content

Bit Masking

mental model using the name

The word mask in the name helps to understand and remember the concept.

Here, mask means a bit pattern that hides all but certain bits. We extract the values from the bit positions we want.

Why bit masking is needed​

Bit masking avoids parsing the whole bit sequence to extract data. That's costly.

For example, file permissions are stored for 3 scopes: owner, group, and others. Without a bit mask, the client must parse the whole permission sequence to get one scope.

With a bitmask, you AND the actual bits with the mask. The mask has ones at the positions you want. The result then lets you read those bits directly.