peterolson / BigInteger.js

An arbitrary length integer library for Javascript
The Unlicense
1.12k stars 187 forks source link

Arithmetic bit shift operators don't produce similar results #232

Closed alexdonh closed 2 years ago

alexdonh commented 2 years ago

As of now BigInt isnt natively supported on React-Native Android so I am trying to use this as a polyfill. I could be wrong but it seems arithmetic bit shift operators don't work correctly.

const BigInt = require('big-integer');
const a = BigInt(80);
const b = BigInt(32);
console.log(a >> b); // This prints 0n on native BigInt, but 80 on this BigInt.
console.log(a.shiftRight(b)); // This prints Integer { value: 0n } expectedly though

Seems like I would need to use shiftLeft and shiftRight instead? Thats gonna be tough as BigInt is being used in another encoding lib that I wouldnt want to manually modify.

peterolson commented 2 years ago

Yes, you need to use the shiftRight and shiftLeft operators. It is impossible to override the >> operator in Javascript.