masking : Java Glossary

go to home page M words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish by Roedy Green ©1996-2009 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 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 (all)
masking
The process of taking apart an int or long into subfields of bits, or clearing the bits of such a subfield. Strictly speaking, masking just refers to stripping off excess bits by doing a logical AND with a mask that contains ones where you want to keep data and zeros where you want it cleared off.

You need skill with the various bit operators to pack several fields into a single int and then unpack them again. You do this work with >>>, <<, &, | and ~ . Rarely you might use the signed shift operator >>. For example to extract the low order three bits you mask with binary 00000111, e.g. z = x & 0x07 . To extract bits 4 and 5 you shift and mask, e.g. z = x >>> 4 & 0x03 . To put together a 2-bit x field in bits 4 and 5 and a 3-bit y field in bits 0, 1, 2 you use code like this: z = x << 4 | y . To zero out a the x field you would take the mask for bits 4 and 5, binary 110000, and invert it, and then mask with that, e.g. z &= ~0x30;

Simple Masking

Shift Masking

Combining Fields

To pack subfields into an int or long, you first get them shifted in to position, then OR them into place.
// To put together a 2-bit x field in bits 4 and 5
// and a 3-bit y field in low order bits 0, 1, 2 you use code like this:
long z = ( x << 3 ) | y;

Creating Masks Dynamically

You can create powers of two, (one-bit masks) by shifting 1 left a variable amount, e.g. (1<<n). Normally you create masks with hex literals, e.g. 0x007f. To dynamically create a mask with n low order 1’s, use (1<<n)-1. Beware, for int this will not work for n=32 since shifts are done modulo 32.

Bit Testing

After you have extracted your bits, often you want to to know if they are all 0 as all 1s or mixed.

IP Tricks

Here are some practical examples of masking, working with IP addresses.

BitSet

java.util.BitSet lets you work with arbitrarly long strings of bits, examining and setting them individually. Inside it is implemented as an array of longs. The class handles all the shifting and masking for you. This is usually the way to fly for anything longer than 64 bits.

CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.62] The information on this page is for non-military use only.
You are visitor number 18,042. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/masking.html J:\mindprod\jgloss\masking.html