Masking the Easy Way

Mike Horn
Back to Index page

Masking allows us to isolate individual bits within the packet. It is based on an AND operator.
The way an AND operator works is very simple so let's start with it.
If you are comparing two bits together (A and B) with an AND operator. You can have four possible results or states. Here is the truth table so you can see this:

A B Result Explanation
0 0 0 If A and B are zero the result is zero.
0 1 0 If A is zero and B is 1 the result is zero.
1 0 0 If A is 1 and B is zero the result is zero.
1 1 1 If A is 1 and B is 1 the result is 1

The only result that has a 1 is when both A and B are 1. Masking uses this by comparing a 1 (masking-in) with the bit of interest and comparing the rest of the bits with a zero (masking-out).
Let's look at an example:
Say you have a four bit value (nibble) but you only care if the most significant bit (MSB) is 1.
You would mask this nibble with 1000 binary (b) so that anything other than the MSB will be zero.
Here is a table so you can see this:
The nibble is 1010b and our mask is 1000b

1 0 1 0 Nibble of Interest
1 0 0 0 Our mask
1 0 0 0 Result


What if our nibble was 0110b ?
Nibble 0110b
Mask 1000

0 1 1 0 Nibble of Interest
1 0 0 0 Our mask
0 0 0 0 Result

The result in the above examples depended on the value of the MSB of our nibble of interest.
The rest of the bits were "zeroed" or "masked-out" by the mask.

Back to Index page

Valid HTML 4.01! Viewable With Any Browser Creative Commons License