/**
* Class to fix the Shiftless Shift problem:
* Java's shift operators work only for shifts
* of 0 to 31 bits.
*
* @author Jonathan Finn
* @version 1.0
* @since 2003-06-20
*/
public class Shifter
{
/**
* Returns the value of i shifted right by 'distance' bits.
* This gives better results than >>> when distance falls
* outside the range 0 to 31.
* Negative values of distance produce a left shift.
* All bits 'shifted in' are zero, which means that for all values
* of distance outside the range -31 .. +31 this returns 0.
*
* @param i Number to shift
* @param distance how may bits to shift. May be negative
* @return i logically shifted right distance bits.
*/
public static int logicalRightShift ( int i , int distance )
{
if ( distance >= 0 )
{
return( distance < 32 ) ? ( i >>> distance ) : 0;
}
else
{
return( distance >= 32 ) ? ( i << - distance ) : 0;
}
}
/**
* Returns the value of i shifted right arithmetically
* by 'distance' bits. This means that bit 31 is always returned
* unchanged, and only bits 0-30 shift.
* This gives better results than >> when distance falls outside
* the range 0 to 31.
* Negative values of distance produce a sign-preserving left shift.
* When shifting right, all bits 'shifted in' are the same as bit
* 31; when shifting left, all bits 'shifted in' are zero.
*
* @param i The number to shift
* @param distance The distance to shift. May be negative
* @return i arithmetically shifted right distance bits.
*/
public static int arithmeticRightShift ( int i , int distance )
{
if ( distance >= 0 )
{
return( distance < 32 ) ? ( i >> distance ) : ( i >> 31 );
}
else
{
final int signBit = i & 0x80000000;
return( distance >= 32 ) ? ( signBit | ( ( i <- < distance ) & ~0x80000000 ) ) : signBit;
}
}
}