Java Number Swap swapBytes(final int i)

Here you can find the source of swapBytes(final int i)

Description

Swap bytes

License

Open Source License

Parameter

Parameter Description
i the integer value to swap

Return

The byte swapped version of i.

Declaration

public static int swapBytes(final int i) 

Method Source Code

//package com.java2s;
/*//w w w.j  a  v  a  2s  .co m
 * Copyright (c) 2014 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial API and implementation
 */

public class Main {
    /**
     * Swap bytes
     *
     * @param s the short value to swap
     * 
     * @return The byte swapped version of <code>s</code>.
     */
    public static short swapBytes(final short s) {
        return (short) ((s << 8) | ((s >> 8) & 0x00ff));
    }

    /**
     * Swap bytes
     *
     * @param i the integer value to swap
     * 
     * @return The byte swapped version of <code>i</code>.
     */
    public static int swapBytes(final int i) {
        return (i << 24) | ((i << 8) & 0x00ff0000) | (i >>> 24) | ((i >> 8) & 0x0000ff00);
    }
}

Related

  1. swap32(int rgb)
  2. swapAddresses(T o1, T o2)
  3. swapBits(byte in)
  4. swapBits(int b, int i, int j)
  5. swapByte(byte b)
  6. swapCasing(String str)
  7. swapDouble(double value)
  8. swapDouble(double value)
  9. swapFloat(float floatValue)