Java Number Swap swap(short myShort)

Here you can find the source of swap(short myShort)

Description

Swaps byte order, short.

License

Creative Commons License

Parameter

Parameter Description
myShort short to have bytes swapped

Return

short w/ bytes of s swapped

Declaration

public static short swap(short myShort) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

public class Main {
    /**/*from   w  ww.j av a2  s.  c  o m*/
     * Swaps byte order, integer.
     *
     * @param myInt int to have bytes swapped
     * @return int w/ bytes of i swapped
     */
    public static int swap(int myInt) {
        return (((myInt & 0xff000000) >> 24) & 0x000000ff) | ((myInt & 0x00ff0000) >> 8)
                | ((myInt & 0x0000ff00) << 8) | ((myInt & 0x000000ff) << 24);
    }

    /**
     * Swaps byte order, short.
     * 
     * @param myShort short to have bytes swapped
     * @return short w/ bytes of s swapped
     */
    public static short swap(short myShort) {
        return (short) (((myShort & 0xff00) >> 8) | ((myShort & 0x00ff) << 8));
    }

    /**
     * Swaps byte order, double.
     * 
     * @param myDouble double to have bytes swapped
     * @return double w/ bytes of d swapped
     */
    public static double swap(double myDouble) {
        long lng = Double.doubleToRawLongBits(myDouble);
        long sl = ((((lng & 0xff00000000000000L) >> 56) & 0x00000000000000ff) | ((lng & 0x00ff000000000000L) >> 40)
                | ((lng & 0x0000ff0000000000L) >> 24) | ((lng & 0x000000ff00000000L) >> 8)
                | ((lng & 0x00000000ff000000L) << 8) | ((lng & 0x0000000000ff0000L) << 24)
                | ((lng & 0x000000000000ff00L) << 40) | ((lng & 0x00000000000000ffL) << 56));
        return Double.longBitsToDouble(sl);
    }
}

Related

  1. swap(final int value)
  2. swap(int elementWidth, long value)
  3. swap(Integer i, Integer j)
  4. swap(short value)
  5. swap(short x)
  6. swap(String str, int i, int j)
  7. swap(StringBuffer s, int i, int j)