Replace NEG with BNOT

This commit is contained in:
nicksavers 2014-10-28 14:25:34 +01:00
parent 056bea7f92
commit bf8163da7a
4 changed files with 18 additions and 13 deletions

View File

@ -151,6 +151,11 @@ public class DataWord implements Comparable<DataWord> {
if (this.data[i] != 0) break; if (this.data[i] != 0) break;
} }
} }
public void bnot() {
if (this.isZero()) return;
this.data = ByteUtil.copyToArray(MAX_VALUE.subtract(this.value()));
}
// By : Holger // By : Holger
// From : http://stackoverflow.com/a/24023466/459349 // From : http://stackoverflow.com/a/24023466/459349

View File

@ -32,7 +32,7 @@ public enum OpCode {
/** (0x08) Exponential operation */ /** (0x08) Exponential operation */
EXP(0x08, 2), EXP(0x08, 2),
/** (0x09) Negation operation */ /** (0x09) Negation operation */
NEG(0x09, 1), BNOT(0x09, 1),
/** (0x0a) Less-than comparison */ /** (0x0a) Less-than comparison */
LT(0X0a, 2), LT(0X0a, 2),
/** (0x0b) Greater-than comparison */ /** (0x0b) Greater-than comparison */

View File

@ -282,9 +282,9 @@ public class VM {
program.stackPush(word1); program.stackPush(word1);
program.step(); program.step();
} break; } break;
case NEG:{ case BNOT:{
DataWord word1 = program.stackPop(); DataWord word1 = program.stackPop();
word1.negate(); word1.bnot();
if (logger.isInfoEnabled()) if (logger.isInfoEnabled())
hint = "" + word1.value(); hint = "" + word1.value();

View File

@ -998,12 +998,12 @@ public class VMTest {
} }
} }
@Test // NEG OP @Test // BNOT OP
public void testNEG_1() { public void testBNOT_1() {
VM vm = new VM(); VM vm = new VM();
program = new Program(Hex.decode("600109"), invoke); program = new Program(Hex.decode("600109"), invoke);
String expected = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"; String expected = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE";
vm.step(program); vm.step(program);
vm.step(program); vm.step(program);
@ -1011,12 +1011,12 @@ public class VMTest {
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase()); assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
} }
@Test // NEG OP @Test // BNOT OP
public void testNEG_2() { public void testBNOT_2() {
VM vm = new VM(); VM vm = new VM();
program = new Program(Hex.decode("61A00309"), invoke); program = new Program(Hex.decode("61A00309"), invoke);
String expected = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5FFD"; String expected = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5FFC";
vm.step(program); vm.step(program);
vm.step(program); vm.step(program);
@ -1024,8 +1024,8 @@ public class VMTest {
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase()); assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
} }
@Test // NEG OP @Test // BNOT OP
public void testNEG_3() { public void testBNOT_3() {
VM vm = new VM(); VM vm = new VM();
program = new Program(Hex.decode("61000009"), invoke); program = new Program(Hex.decode("61000009"), invoke);
@ -1037,8 +1037,8 @@ public class VMTest {
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase()); assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
} }
@Test(expected=StackTooSmallException.class) // NEG OP @Test(expected=StackTooSmallException.class) // BNOT OP
public void testNEG_4() { public void testBNOT_4() {
VM vm = new VM(); VM vm = new VM();
program = new Program(Hex.decode("09"), invoke); program = new Program(Hex.decode("09"), invoke);