Skip to content

Commit c1785aa

Browse files
committed
Try to fix right shift
1 parent 4e3ad4e commit c1785aa

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/main/java/at/favre/lib/bytes/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ static byte[] shiftRight(byte[] data, int shiftBitCount) {
402402
data[i] = 0;
403403
} else {
404404
byte src = data[sourceIndex];
405-
byte dst = (byte) (src >> shiftMod);
405+
byte dst = (byte) ((0xff & src) >>> shiftMod);
406406
if (sourceIndex - 1 >= 0) {
407407
dst |= data[sourceIndex - 1] << (8 - shiftMod) & carryMask;
408408
}

src/test/java/at/favre/lib/bytes/UtilTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ public void testLeftShift() {
255255
@Test
256256
public void testRightShift() {
257257
byte[] test = new byte[]{0, 0, 16, 0};
258+
assertEquals(0b01111110, 0b11111101 >>> 1);
259+
assertArrayEquals(new byte[]{0b00101101, (byte) 0b01111110}, Util.shiftRight(new byte[]{0b01011010, (byte) 0b11111101}, 1));
258260
assertArrayEquals(new byte[]{0, -128, -128, -128}, Util.shiftRight(new byte[]{1, 1, 1, 1}, 1));
259261
assertArrayEquals(new byte[]{0, -128, 66, 0}, Util.shiftRight(new byte[]{2, 1, 8, 2}, 2));
260262
assertArrayEquals(new byte[]{0, -128, 66, 0}, new BigInteger(new byte[]{2, 1, 8, 2}).shiftRight(2).toByteArray());
@@ -275,8 +277,8 @@ public void testRightShift() {
275277
byte[] actual = Util.shiftRight(rnd.copy().array(), shift);
276278

277279
System.out.println("Original \t" + rnd.encodeBinary());
278-
System.out.println("Expected \t " + Bytes.wrap(expected).encodeBinary());
279-
System.out.println("Actual \t " + Bytes.wrap(actual).encodeBinary() + "\n\n");
280+
System.out.println("Expected \t" + Bytes.wrap(expected).encodeBinary());
281+
System.out.println("Actual \t" + Bytes.wrap(actual).encodeBinary() + "\n\n");
280282

281283
assertArrayEquals(expected, actual);
282284
}

0 commit comments

Comments
 (0)