peterolson / BigInteger.js

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

some edge cases for `shiftRight` and `shiftLeft` #143

Closed Yaffle closed 6 years ago

Yaffle commented 6 years ago

Is it possible to change the behavior of shiftRightand shiftLeft to match how BigInt works (https://github.com/tc39/proposal-bigint) with large second argument?

bigInt(0).shiftRight(-100000000000000000000) should give bigInt(0) bigInt(0).shiftLeft(100000000000000000000) should give bigInt(0) bigInt(5).shiftRight(100000000000000000000) should give bigInt(0) bigInt(5).shiftLeft(-100000000000000000000) should give bigInt(0) bigInt(-5).shiftRight(100000000000000000000) should give bigInt(-1) bigInt(-5).shiftLeft(-100000000000000000000) should give bigInt(-1)

peterolson commented 6 years ago

I'm looking at the specification for it, and I can't find this behavior documented anywhere. Am I missing it somewhere?

I'm curious why they would want to return 0 and 1 for large arguments? Do you have any idea?

Yaffle commented 6 years ago

just because the operation "a << b" works only when both args are bigints (as all other Arithmetic and Bitwise operations). and 0n >> -100000000000000000000n is equal to 0 * 2**100000000000000000000...