/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package usda.weru.util.gis;

/**
 *
 * @author wjr
 */
 public class Swab {

	/**
	 *
	 * @param v
	 * @return
	 */
	public final static int swab(int v) {
     return  (v >>> 24) | (v << 24) | 
       ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00);
     }

	/**
	 *
	 * @param v
	 * @return
	 */
	public final static short swab(short v) {
       return (short) (((v >>> 8) & 0xff) | (v << 8));
   }

	/**
	 *
	 * @param v
	 * @return
	 */
	public final static double swab(double v) {
       long ibits = Double.doubleToLongBits(v);
       long obits = ibits >>> 56;
       obits |= (ibits >>> 40) & 0x000000000000ff00L;
       obits |= (ibits >>> 24) & 0x0000000000ff0000L;
       obits |= (ibits >>>  8) & 0x00000000ff000000L;
       obits |= (ibits <<   8) & 0x000000ff00000000L;
       obits |= (ibits <<  24) & 0x0000ff0000000000L;
       obits |= (ibits <<  40) & 0x00ff000000000000L;
       obits |= (ibits <<  56) & 0xff00000000000000L;
       return Double.longBitsToDouble(obits);
     }

	/**
	 *
	 * @param argv
	 */
	public static void main(String argv[]) {
     //  before 0x01020304
     //    after  0x04030201
     int v = 0x01020304;
     System.out.println("before : 0x" + Integer.toString(v,16));
     System.out.println("after  : 0x" + Integer.toString(swab(v),16));
     }
   }
   
