Mask (computing)
From Free net encyclopedia
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 counting along from the most significant bit, we would use a bitmask such as 000010000 and use the bitwise AND operator. Recalling that 1 AND 1 = 1, with 0 otherwise, we find the status of the fifth bit, since
100111010 AND 000010000 = 000010000
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 bitwise operation (NOT x) AND y, a Template:Num in the mask instructs that the binary datum below should be ignored, while Template:Nums 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 device's IP address and used to instruct a router which bits of the address indicate the subdivision of the network the computer is on and which identify the specific computer within the subnetwork.
Contents |
Common bitmask functions
Masking bits to 1
To turn certain bits on, we use the bitwise OR operation. Recall that Y or 1 = 1 and Y or 0 = Y. Therefore, to make sure a bit is on, we OR it with a 1. To leave a bit alone, we OR it with a 0. For example, to turn on bit-1 (the second bit to the left of the binary point) for a value,
Masking bits to 0
As we see above, there's no way to change a bit from on to off using the OR operation. Instead, we use bitwise AND. When a value is ANDed with a 1, the result is simply the original value as in: Y and 1 = Y. However, when we AND a value with 0, we're gaurunteed to get a 0 back so we can turn a bit off bit ANDing it with 0: Y and 0 = 0.
Querying the status of a bit
You can also use bitmasks to easily check the state of individual bits regardless of the other bits. To do this, you simply turn off all the other bits using the bitwise AND as discussed above and see if the resulting value is 0. If it is, then the bit was not on, but if the value is any other value, then the bit was on. What makes this so convenient is that you don't need to figure out what the value actually is, you just need to know that it isn't 0.
Toggling bit values
So far we've seen how to turn bits on and turn bits off, but not both at once. What if we don't really care what the value is, we just know we want it to be the opposite of what it currently is? We can do this using the XOR (exclusive OR) operation. XOR returns 1 if and only if an odd number of bits are 1. Therefore, if two corresponding bits are 1, the result will be a 0, but if only one of them is 1, the result will be 1. Therefore we can invert the values of bits by XORing them with a 1. if the original bit was 1, we'll get 1 xor 1 = 0. If the original bit was 0 we'll get 0 xor 1 = 1. Also note that XOR masking is bit-safe, meaning it won't effect unmasked bits because Y xor 0 = Y, just like an OR.de:Bitmaske ro:Mascare binară ru:Битовая маска