Download this source file
// Pack.java - Demo of packing two ints into one long for efficiency.
// Written April 2000 by Wayne Pollock, Tampa Florida USA.
class Pack
{
public static void main ( String [] args )
{ int i = 17, j = 22;
long mask = -1L >>> 32; // 32 zeros, then 32 ones: 0x00000000FFFFFFFF
// Note the >>> operator shifts in zeros.
if ( args.length == 2 )
{ i = Integer.parseInt( args[0] );
j = Integer.parseInt( args[1] );
}
long l = ( (long) i << 32 ) | ( j & mask );
System.out.println( "packed values of " + i + " and " + j + ": " + l );
int x = (int) ( l >> 32 );
int y = (int) ( l & mask );
System.out.println( "Extracted values are " + x + " and " + y );
}
}