1

I am using the jsbn library to manage BigIntegers in a javascript application. It seems that the negate function is not working well.

I expect that the negate function works like the Java one.

BigInteger minusOne = BigInteger.ONE.negate(); // -1

But with the jsbn library the following code produce this result...

var BigInteger = require('jsbn').BigInteger;

var bi = BigInteger.ONE;
console.log(bi); // 1
console.log(bi.negate()); // 268435455 but should be -1, no ??

You can try this code here https://runkit.com/gikoo/jsbn-negate-function/1.0.0

91K00
  • 452
  • 4
  • 7

1 Answers1

1

BigInteger is storing numbers in a way that allows them to track numbers bigger than what JavaScript can track. How they do that you should consider a black box - when you are ready to go back to a normal int, you need to do bi.negate().intValue(), or if it really is too big, bi.negate().toString()

https://runkit.com/davidjwilkins/example-bigint

dave
  • 62,300
  • 5
  • 72
  • 93
  • Thank you! I was wrong, I was looking at the representation of the value in jsbn instead of dealing with the value directly. – 91K00 Jul 07 '21 at 21:12