Fix for SIGNEXTEND

This commit is contained in:
nicksavers 2014-11-06 15:45:07 +01:00
parent cde8567d4b
commit 370ec383c7
2 changed files with 31 additions and 1 deletions

View File

@ -295,7 +295,7 @@ public class DataWord implements Comparable<DataWord> {
}
public void signExtend(byte k) {
if (k > 31)
if (0 > k || k > 31)
throw new IndexOutOfBoundsException();
byte mask = this.sValue().testBit((k * 8) + 7) ? (byte) 0xff : 0;
for (int i = 31; i > k; i--) {

View File

@ -216,6 +216,36 @@ public class DataWordTest {
System.out.println(x.toString());
assertEquals(expected, x.toString());
}
@Test
public void testSignExtend8() {
byte k = 30;
DataWord x = new DataWord(Hex.decode("ff34567882345678823456788234567882345678823456788234567882345678"));
String expected = "0034567882345678823456788234567882345678823456788234567882345678";
x.signExtend(k);
System.out.println(x.toString());
assertEquals(expected, x.toString());
}
@Test(expected=IndexOutOfBoundsException.class)
public void testSignExtendException1() {
byte k = -1;
DataWord x = new DataWord();
x.signExtend(k); // should throw an exception
}
@Test(expected=IndexOutOfBoundsException.class)
public void testSignExtendException2() {
byte k = 32;
DataWord x = new DataWord();
x.signExtend(k); // should throw an exception
}
public static BigInteger pow(BigInteger x, BigInteger y) {
if (y.compareTo(BigInteger.ZERO) < 0)