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
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