| |||||||||
In computer science, a mask is some data that, along with an operation, are used in order to extract information stored elsewhere.
The most common mask used, also known as a bitmask, extracts the status of certain bits in a binary string or number. For example, if we have the binary string 100111010 and we want to extract the status of the fifth bit from the most significant bit, we would use a bitmask such as 000010000 and use the logical AND operator. Recalling that a AND a = 1, with 0 otherwise, we obtain the status of the fifth bit since
Likewise we can set the fifth bit by applying the mask to the data using the OR operator.
Similarly, we can use a sequence of binary numbers with a piece of data of equal length used to inform as to what parts of the data should be examined. With the logical operator (NOT x) AND y, a 1 in the mask instructs that the binary datum below should be ignored, while 0s in the mask tell that the data below are to be examined. A common type of mask of this type is a subnetwork mask, which is associated with a computer's IP address and used to tell a router as to how much of the address indicates the subdivision of the network the computer is on and how much indicates which computer within the particular subnetwork it is.