1. Home
  2. Computing & Technology
  3. C / C++ / C#

Definition of Masking

By David Bolton, About.com

Definition: Masking is the process of changing bits in a byte.

We start with a Value 01010011 which we wish to combine with Data 1DD10DD1. We only wish to alter the bits in the Data that are not 'D's. These could be 0 or 1s, but that is not important- we just wish to leave them unchanged. We'll use masking to do this.

  1. First we generate a mask from the Data. This has a 0 in every bit position we can change in the Data, a 1 in every bit position we do not want to change. So this is 01100110. We AND (&) this with the Data to clear every bit that can be changed and leave the rest intact. 0DD00DD0.
  2. Next we generate the invert of the Mask which is 10011001 and we AND (&) this with the Value (01010011) with this to produce 00010001.
  3. Finally we OR (|) this (00010001) with the masked data (0DD00DD0) to give the final value 0DD10DD1.
01010011 - Value
1DD10DD1 - Data

01100110 - Mask (From Data)
0DD00DD0 - (Mask & Data)

100111001 - Inverted Mask

00010001 - (Inverted Mask & Value)
--------------------
0DD10DD1 - (Inverted Mask & Value) | (Mask & Data)

Glossary:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#

©2009 About.com, a part of The New York Times Company.

All rights reserved.