I was thinking of a way to implement a NOT
operation/instruction for IJVM so that I could write a multiplication JAS method, but I'm having trouble trying to implement a negation method.
Can anyone help out with a way to go about doing this?
Basically there are various ways to calculate the one's complement of a value, i.e. NOT
:
not_x = NAND(x, x) = NAND(x, ~0);
not_x = NOR(x, x) = NOR(x, 0);
not_x = -x - 1; // because in 2's complement -x = ~x + 1
not_x = 0xFFFFFFFF - x; // assuming 32-bit computer
not_x = x XOR 0xFFFFFFFF; // or x XOR ~0
...
I don't know about IJVM but as described here it supports only 4 arithmetic operations IADD
, ISUB
, IAND
and IOR
. You can use ISUB
to achieve this
Now for more fun we can do not_x = x XOR ~0 = (x OR ~0) - (x AND ~0)
since a XOR b = (a OR b) - (a AND b)
. An alternative solution is to use a lookup table