Example usage for java.lang Long reverse

List of usage examples for java.lang Long reverse

Introduction

In this page you can find the example usage for java.lang Long reverse.

Prototype

public static long reverse(long i) 

Source Link

Document

Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified long value.

Usage

From source file:Main.java

public static void main(String[] args) {
    System.out.println(Long.reverse(123123));
}

From source file:com.offbynull.voip.kademlia.model.BitString.java

private static byte readBitsFromByteArrayInReadOrder(byte[] container, int offset, int len) {
    Validate.isTrue(len <= 8);/*  w  w w.  j av  a2  s .  c  o  m*/
    Validate.isTrue(offset + len <= container.length * 8);

    int byteOffset = offset / 8;
    int bitOffset = offset % 8;

    int idx = byteOffset;

    int lenOfBitsRemainingInByte = 8 - bitOffset;

    byte ret;
    if (len <= lenOfBitsRemainingInByte) {
        long byte1 = Long.reverse(container[idx] & 0xFFL) >>> 56;
        ret = (byte) (isolateBitsToBottom(byte1, bitOffset, len) & 0xFFL);
    } else {
        long byte1 = Long.reverse(container[idx] & 0xFFL) >>> 56;
        long byte2 = Long.reverse(container[idx + 1] & 0xFFL) >>> 56;
        int byte1BitOffset = bitOffset;
        int byte1BitLen = Math.min(8 - bitOffset, len);
        int byte2BitOffset = 0;
        int byte2BitLen = len - byte1BitLen;

        long portion1 = isolateBitsToBottom(byte1, byte1BitOffset, byte1BitLen);
        long portion2 = isolateBitsToBottom(byte2, byte2BitOffset, byte2BitLen);

        long combined = (portion1 << byte2BitLen) | portion2;

        ret = (byte) (combined & 0xFF);
    }

    return ret;
}

From source file:com.t_oster.liblasercut.drivers.LaosCutter.java

/**
 * This Method takes a raster-line represented by a list of bytes,
 * where: byte0 ist the left-most byte, in one byte, the MSB is the
 * left-most bit, 0 representing laser off, 1 representing laser on.
 * The Output List of longs, where each value is the unsigned dword
 * of 4 bytes of the input each, where the first dword is the leftmost
 * dword and the LSB is the leftmost bit. If outputLeftToRight is false,
 * the first dword is the rightmost dword and the LSB of each dword is the
 * the Output is padded with zeroes on the right side, if leftToRight is true,
 * on the left-side otherwise/*  w  w w  . jav  a2  s  . co m*/
 * rightmost bit
 * @param line
 * @param outputLeftToRight
 * @return
 */
public List<Long> byteLineToDwords(List<Byte> line, boolean outputLeftToRight) {
    List<Long> result = new ArrayList<Long>();
    int s = line.size();
    for (int i = 0; i < s; i++) {
        line.set(i, (byte) (Integer.reverse(0xFF & line.get(i)) >>> 24));
    }
    for (int i = 0; i < s; i += 4) {
        result.add((((long) (i + 3 < s ? 0xFF & line.get(i + 3) : 0)) << 24)
                + (((long) (i + 2 < s ? 0xFF & line.get(i + 2) : 0)) << 16)
                + (((long) (i + 1 < s ? 0xFF & line.get(i + 1) : 0)) << 8) + ((long) (0xFF & line.get(i))));
    }
    if (!outputLeftToRight) {
        Collections.reverse(result);
        for (int i = 0; i < result.size(); i++) {
            result.set(i, Long.reverse(result.get(i)) >>> 32);
        }
    }
    return result;
}