|
Bit Masking (Setting, Clearing and Inverting) Using Boolean Logical Functions
(Page 3 of 3)
Inverting Groups of Bits Using the XOR Function
There are also situations in which
we want to invert some bits; that is, change a one value to a
zero, or a zero value to a one. To do this, we use the XOR function.
While this is not as intuitive as the way masking works with OR
and AND, if you refer to the XOR truth table (Table 13)
you will see that if you XOR with a one, the input value is flipped,
while XORing with a zero causes the input to be unchanged. To
see how this works, let's take the same input example and invert the
middle six bits, as shown in Table 16.
Table 16: Inverting Bits Using an XOR Bit Mask
Input
|
1
|
0
|
1
|
0
|
0
|
1
|
0
|
1
|
1
|
0
|
1
|
0
|
Mask
|
0
|
0
|
0
|
1
|
1
|
1
|
1
|
1
|
1
|
0
|
0
|
0
|
Result of XOR Operation
|
1
|
0
|
1
|
1
|
1
|
0
|
1
|
0
|
0
|
0
|
1
|
0
|
In the world of networking,
bit masking is most commonly used for the manipulation of addresses.
In particular, masking is perhaps best known for its use in differentiating
between the host and subnetwork (subnet) portions of Internet Protocol
(IP) addresses, a process called subnet
masking and described in the
section on IP subnet addressing.
Note: Masks are often expressed in either hexadecimal or decimal notation for simplicity of expression, as shown in the IP subnetting summary tables. However, the masks are always applied in binary, as described above. You should convert the mask to binary if you want to see exactly how the masking operation is going to work. |
Key Concept: The properties of the OR and AND boolean functions make them useful when certain bits of a data item need to be set (changed to 1) or cleared (changed to 0), a process called bit masking. To set bits to one, a mask is created and used in a bit-by-bit OR with the input; where the mask has a value of 1, the bit is forced to a 1, while each 0 bit leaves the corresponding original bit unchanged. Similarly, a mask used with AND clears certain bits; each 1 bit in the mask leaves the original bit alone, while each 0 forces the output to 0. Finally, XOR can be used to invert selected bits using a mask. |

| If you find The TCP/IP Guide useful, please consider making a small Paypal donation to help the site, using one of the buttons below. You can also donate a custom amount using the far right button (not less than $1 please, or PayPal gets most/all of your money!) In lieu of a larger donation, you may wish to consider purchasing a download license of The TCP/IP Guide. Thanks for your support! |
|
|
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.
|