1

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?

phuclv
  • 37,963
  • 15
  • 156
  • 475

1 Answers1

1

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

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Thanks for the help! – David Andrews Apr 10 '16 at 04:31
  • Heh, I'm so used to x86 / SSE that I was thinking `NAND(x, x);` would always be zero. I was thinking of `ANDN` (which NOTs one input before AND), rather than `NAND` (which NOTs the output of an AND). – Peter Cordes Sep 22 '18 at 12:55
  • 1
    @PeterCordes yes. Among the architectures I know only MIPS has `nor` and a `not` will be done by `nor $dst, $src, $zero`. IMHO `nand` or `andn` would be more useful – phuclv Sep 22 '18 at 13:39