Bit shift operations on a long array in Java
Asked Answered
R

1

0

How does one shift a long array n positions to the right or left e.g. do a >>> n or << n on this array

long[] values = new long[]{
            0b1011010011111111001100111000000100110011101101110010001011100001L,
            0b1011010011111111001100111000000100110011101101110010001011100001L}; 

Disclaimer
This question was asked since it doesn't exist, it is an extension of this.

Rhinelandpalatinate answered 8/3, 2023 at 21:13 Comment(0)
R
1

According to Patrick's answer here, for a long[] would be

Left Shift <<

public static long[] shiftLeft(long[] longArray, int shiftBitCount) {
    final int shiftMod = shiftBitCount % 64;
    final long carryMask = ((1 << shiftMod) - 1);
    final int offsetLongs = (shiftBitCount / 64);

    int sourceIndex;
    for (int i = 0; i < longArray.length; i++) {
        sourceIndex = i + offsetLongs;
        if (sourceIndex >= longArray.length) {
            longArray[i] = 0;
        } else {
            long src = longArray[sourceIndex];
            long dst = (long) (src << shiftMod);
            if (sourceIndex + 1 < longArray.length) {
                dst |= longArray[sourceIndex + 1] >>> (64 - shiftMod) & carryMask;
            }
            longArray[i] = dst;
        }
    }
    return longArray;
}

Unsigned Right Shift >>>

public static long[] shiftRight(long[] longArray, int shiftBitCount) {
    final int shiftMod = shiftBitCount % 64;
    final long carryMask = 0xFFFFFFFFFFFFFFFFL  << (64 - shiftMod);
    final int offsetLongs = (shiftBitCount / 64);

    int sourceIndex;
    for (int i = longArray.length - 1; i >= 0; i--) {
        sourceIndex = i - offsetLongs;
        if (sourceIndex < 0) {
            longArray[i] = 0;
        } else {
            long src = longArray[sourceIndex];
            long dst =  src >>> shiftMod;
            if (sourceIndex - 1 >= 0) {
                dst |= longArray[sourceIndex - 1] << (64 - shiftMod) & carryMask;
            }
            longArray[i] = dst;
        }
    }
    return longArray;
}

Example

  public static void main(String[] args) {
        int shift = 18;//n
        long[] values = new long[]{
                0b1011010011111111001100111000000100110011101101110010001011100001L,
                0b1011010011111111001100111000000100110011101101110010001011100001L};
        show("input: ",values);
        
        shiftRight(values,shift);
        show("output{>>>"+shift+"}: ",values);
        //shiftLeft(values,shift);
        //show("output{ <<"+shift+"}: ",values);
    }
    
    public static void show(String message,long[] values){
        String input = "";
        for (int i = 0; i < values.length; i++) {
            long value = values[i];
            input += String.format("%64s", Long.toBinaryString(value)).replace(' ', '0');
        }System.out.println(message+input);
    }
Rhinelandpalatinate answered 8/3, 2023 at 21:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.