Java Glossary : masking

CMP home Java glossary home Menu no menu Last updated 2004-06-30 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 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

You are here : home : Java Glossary : M words : masking.

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

// basic mask, get the low order bit.  e.g. 1 for odd, 0 for even.
int loBit = i & 0x1;
// or
boolean odd = ( i & 0x1 ) != 0;

// get low order 3 bits.
int lo3Bits = i & 0x7;

// get low order nibble, 4 bits.
int loNibble = i & 0xf;

// get low order byte, 8 bits:
int loByte = i & 0xff;
// or
byte loByte = (byte)i;

Shift Masking

// shift plus mask with &

// get sign bit
int signBit = ( i >>> 31 ) & 0x1;
// or
boolean negative = i < 0;

// get second lowest 3 bits.
int second3Bits = ( i >>> 3 ) & 0x7;

// get high order nibble, 4 bits.
int hiNibble = ( i >>> 28 ) & 0xf;

// get high order byte, 8 bits:
int hiByte = ( i >>> 24 ) & 0xff;
// or
byte hiByte = (byte)( i >>> 24 );

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

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.
// create a mask of 1s 24 bits wide.
int mask24 = 0xffffff;
// or
int mask24 = ( 1 << 24 ) - 1;  // watch out, formula does not work for 32

// create a mask of 0s 24 bits wide, the rest 1s
int mask024 = 0xff000000;
// or
int mask024 = ~0xffffff;
// or
int mask024 = ~( ( 1 << 24 ) - 1 );  // watch out, formula does not work for 32

// create a mask of ones to extract the second lowest byte
int mask2 = 0x0000ff00;
// or
int mask2 = ( ( 1 << 8 ) - 1 ) >>> 8;

// create  mask for clearing the second lowest byte
int clear2 = 0xffff00ff;
// or
int clear2 = ~(( ( 1 << 8 ) - 1 ) >>> 8 );

// use it to clear that byte and leave everything else alone with
int v &= clear2;

IP Tricks

Here are some practical examples of masking, working with IP addresses.
// IP bit tricks
// compute an n bit subnet mask, all 1s except for last n bits.
int subnet = ~( ( 1 << n ) - 1 );

// generate a list of possible local IP addresses, e.g.
// 192.168.0.0 .. 192.168.255.255

int base = (192 << 24) | (168 << 16);
for ( int i=0; i<0xffff; i++ )
   {
   int sample = base | i;
   System.out.println( DottedQuad.dottedQuad( sample ) );
   }
public class DottedQuad
   {
   /**
    * display an IP as a dotted quad xxx.xxx.xxx.xxx
    */
   public static String dottedQuad ( int ip )
      {
      StringBuffer sb = new StringBuffer( 15 );
      for ( int shift=24; shift >0; shift-=8 )
         {
         // process 3 bytes, from high order byte down.
         sb.append( Integer.toString( (ip >>> shift) & 0xff ));
         sb.append('.');
         }
      sb.append( Integer.toString( ip & 0xff ) );
      return sb.toString();
      }

   /**
    * test harness
    *
    * @param args not used
    */
   public static void main ( String[] args )
      {
      System.out.println( dottedQuad( 0x01ff0010 ) );
      }
   }


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 36.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/masking.html J:\mindprod\jgloss\masking.html