| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Bit Masking (Setting, Clearing and Inverting) Using Boolean Logical Functions (Page 1 of 3) The boolean functions NOT, AND, OR and XOR describe different ways that logical expressions can be used to manipulate true and false values to represent both simple and complex decisions or conditions. However, these functions can also be used in a more mundane manner, to allow the direct manipulation of binary data. This use of boolean logic is very important in a number of different applications in networking. As I mentioned in the topic introducing binary numbers, giving a bit a value of one is called setting the bit, while giving it a value of zero is either resetting or clearing it. In some situations bits are handled individually, and are set or cleared simply by assigning a one or zero value to each bit. However, it is common to have large groups of bits that are used collectively to represent a great deal of information, where many bits need to be set or cleared at once. In this situation, the boolean functions come to the rescue. Setting bits en masse can be done by exploiting the properties of the OR function. Recall that the OR function's output is true (one) if any of its inputs is true (one). Thus, if you OR a bit with a value known to be one, the result is always going to be a one, no matter what the other value is. In contrast, if you OR with a zero, the original value, one or zero, is not changed. By using a string with zeroes and ones in particular spots, you can set certain bits to 1 while leaving others unchanged. This procedure is comparable to how a painter masks areas that he does not want to be painted, using plastic or perhaps masking tape. Thus, the process is called masking. The string of digits used in the operation is called the bit mask, or more simply, just the mask. An example will illustrate. Suppose we have the 12-bit binary input number 101001011010, and we want to set the middle six bits to be all ones. To do this, we simply OR the number with the 12-bit mask 000111111000. Table 14 shows how this works, with the changed bits in the result highlightedwe simply OR each bit in the input with its corresponding bit in the mask:
Home - Table Of Contents - Contact Us The TCP/IP Guide (http://www.TCPIPGuide.com) Version 3.0 - Version Date: September 20, 2005 © Copyright 2001-2005 Charles M. Kozierok. All Rights Reserved. Not responsible for any loss resulting from the use of this site. |