Java Number Swap swapShort(final short value)

Here you can find the source of swapShort(final short value)

Description

Converts a "short" value between endian systems.

License

Open Source License

Parameter

Parameter Description
value value to convert

Return

the converted value

Declaration

public static short swapShort(final short value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from w  w  w  .j  ava  2 s .  c o  m
     * Number of bits on a byte.
     */
    public static final int BIT_SIZE_OF_BYTE = 8;
    /**
     * The mask of a byte from an integer. Used to simulate a fast coercion of integer to
     * byte.
     */
    public static final int INT_LOW_BYTE_MASK = 0x000000FF;

    /**
     * Converts a "short" value between endian systems.
     * 
     * @param value value to convert
     * @return the converted value
     */
    public static short swapShort(final short value) {
        final int result = (((value >> 0) & INT_LOW_BYTE_MASK) << BIT_SIZE_OF_BYTE)
                + (((value >> BIT_SIZE_OF_BYTE) & INT_LOW_BYTE_MASK) << 0);
        return (short) result;
    }
}

Related

  1. swapInt(int i)
  2. swapInteger(int integer)
  3. swapLong(long value)
  4. swapLong(long value)
  5. swapLong(long value)
  6. swapShort(short value)
  7. swapShort(short value)