Java Number Swap swapInteger(int integer)

Here you can find the source of swapInteger(int integer)

Description

Byte-swaps the given integer to the other endian.

License

Open Source License

Parameter

Parameter Description
integer the integer to swap

Declaration

public static int swapInteger(int integer) 

Method Source Code

//package com.java2s;
/*/* w  ww . j  av  a2 s . co  m*/
 * Copyright 1999-2002 Carnegie Mellon University.  
 * Portions Copyright 2002 Sun Microsystems, Inc.  
 * Portions Copyright 2002 Mitsubishi Electric Research Laboratories.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 *
 */

public class Main {
    /**
     * Byte-swaps the given integer to the other endian. That is, if this integer is big-endian, it becomes
     * little-endian, and vice-versa.
     *
     * @param integer the integer to swap
     */
    public static int swapInteger(int integer) {
        int swapped = 0, byte1, byte2, byte3, byte4;

        byte1 = (integer >> 0) & 0xff;
        byte2 = (integer >> 8) & 0xff;
        byte3 = (integer >> 16) & 0xff;
        byte4 = (integer >> 24) & 0xff;
        swapped = byte1 << 24 | byte2 << 16 | byte3 << 8 | byte4 << 0;

        return swapped;
    }
}

Related

  1. swapDouble(double value)
  2. swapFloat(float floatValue)
  3. swapFloat(float value)
  4. swapInt(int i)
  5. swapInt(int i)
  6. swapLong(long value)
  7. swapLong(long value)
  8. swapLong(long value)
  9. swapShort(final short value)