Java Number Swap swap(short value)

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

Description

Swap the bytes in the value, thereby converting a big-endian value into a little-endian value (or vice versa).

License

Open Source License

Parameter

Parameter Description
value the value to be swapped

Return

the swapped value

Declaration

protected static short swap(short value) 

Method Source Code

//package com.java2s;
/*/*from   w w w . ja  va  2  s.co m*/
 *  Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */

public class Main {
    static final long ooooooooooooooff = 0x00000000000000ffL;
    static final long ooooooooooooffoo = 0x000000000000ff00L;
    static final long ooooooooooffoooo = 0x0000000000ff0000L;
    static final long ooooooooffoooooo = 0x00000000ff000000L;
    static final long ooooooffoooooooo = 0x000000ff00000000L;
    static final long ooooffoooooooooo = 0x0000ff0000000000L;
    static final long ooffoooooooooooo = 0x00ff000000000000L;
    static final long ffoooooooooooooo = 0xff00000000000000L;

    /** Swap the bytes in the value, thereby converting a big-endian value
     * into a little-endian value (or vice versa).
     * @param value the value to be swapped
     * @return the swapped value
     */
    protected static short swap(short value) {
        return (short) ((0x00ff & (value >>> 8)) | (0xff00 & (value << 8)));
    }

    /** Swap the bytes in the value, thereby converting a big-endian value
     * into a little-endian value (or vice versa).
     * @param value the value to be swapped
     * @return the swapped value
     */
    protected static int swap(int value) {
        return 0x000000ff & (value >>> 24) | (0x0000ff00 & (value >>> 8))
                | (0x00ff0000 & (value << 8))
                | (0xff000000 & (value << 24));
    }

    /** Swap the bytes in the value, thereby converting a big-endian value
     * into a little-endian value (or vice versa).
     * @param value the value to be swapped
     * @return the swapped value
     */
    protected static long swap(long value) {
        return ooooooooooooooff & (value >>> 56)
                | (ooooooooooooffoo & (value >>> 40))
                | (ooooooooooffoooo & (value >>> 24))
                | (ooooooooffoooooo & (value >>> 8))
                | (ooooooffoooooooo & (value << 8))
                | (ooooffoooooooooo & (value << 24))
                | (ooffoooooooooooo & (value << 40))
                | (ffoooooooooooooo & (value << 56));
    }
}

Related

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