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);
}