Please Whitelist This Site?

I know everyone hates ads. But please understand that I am providing premium content for free that takes hundreds of hours of time to research and write. I don't want to go to a pay-only model like some sites, but when more and more people block ads, I end up working for free. And I have a family to support, just like you. :)

If you like The TCP/IP Guide, please consider the download version. It's priced very economically and you can read all of it in a convenient format without ads.

If you want to use this site for free, I'd be grateful if you could add the site to the whitelist for Adblock. To do so, just open the Adblock menu and select "Disable on tcpipguide.com". Or go to the Tools menu and select "Adblock Plus Preferences...". Then click "Add Filter..." at the bottom, and add this string: "@@||tcpipguide.com^$document". Then just click OK.

Thanks for your understanding!

Sincerely, Charles Kozierok
Author and Publisher, The TCP/IP Guide


NOTE: Using software to mass-download the site degrades the server and is prohibited.
If you want to read The TCP/IP Guide offline, please consider licensing it. Thank you.

The Book is Here... and Now On Sale!

Read offline with no ads or diagram watermarks!
The TCP/IP Guide

Custom Search







Table Of Contents  The TCP/IP Guide
 9  Networking Fundamentals
      9  Backgrounder: Data Representation and the Mathematics of Computing

Previous Topic/Section
Boolean Logic and Logical Functions
Previous Page
Pages in Current Topic/Section
1
23
Next Page
The Open System Interconnection (OSI) Reference Model
Next Topic/Section

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 Groups of Bits Using the OR Function

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 highlighted—we simply OR each bit in the input with its corresponding bit in the mask:


Table 14: Setting Bits Using an OR 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 OR Operation

1

0

1

1

1

1

1

1

1

0

1

0


Previous Topic/Section
Boolean Logic and Logical Functions
Previous Page
Pages in Current Topic/Section
1
23
Next Page
The Open System Interconnection (OSI) Reference Model
Next Topic/Section

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!
Donate $2
Donate $5
Donate $10
Donate $20
Donate $30
Donate: $



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.