MikeMcl / bignumber.js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
http://mikemcl.github.io/bignumber.js
MIT License
6.64k stars 741 forks source link

Bitwise methods #281

Closed Vap0r1ze closed 1 month ago

Vap0r1ze commented 3 years ago

Hiya Mike, It's me from #106 I'm opening a fresh PR since I deleted my old fork. I believe I addressed all of your concerns, but it's pretty late so I wouldn't be surprised if I missed something. Let me know what else there is to be done, or any issues you have with the PR in its current state.

MikeMcl commented 3 years ago

Hello again. I'll take a look at this properly this weekend.

MikeMcl commented 3 years ago

A quick first test of bitShiftRight seems to give the wrong answer:

let n = new BigNumber(4);
n = n.bitShiftRight(1);
console.log(n.toString());    // '4'
console.log(4 >> 1);          // 2
Vap0r1ze commented 3 years ago

fixed that

MikeMcl commented 3 years ago
console.log(BigNumber(9).and(57).toString());     // 2
console.log(9 & 57);                              // 9

console.log(BigNumber(8).or(38).toString());      // 11
console.log(8 | 38);                              // 46

console.log(BigNumber(34).xor(89).toString());    // 61 
console.log(34 ^ 89);                             // 123

console.log(BigNumber(84).not().toString());      // NaN 
console.log(~84);                                 // -85
Vap0r1ze commented 3 years ago

oh i forgot tests, that would be important

pwener commented 2 years ago

Hello, can I help this feature?

Vap0r1ze commented 2 years ago

Hello, can I help this feature?

Yea I think you'd just need to write tests dealing with all the methods dealing with special cases too for replicating the behaviour of >> and <<. Afterwards just bug fix from there.

Edit: Feel free to open PR over at Vap0r1ze:bitwise :D

pwener commented 2 years ago

Hi everyone, I submitted an PR at Vap0r1ze:bitwise . I have a doubt, that you can answer too @MikeMcl , we can put all these test together in the same file?

pwener commented 1 year ago

@MikeMcl there is some issue here?

MikeMcl commented 1 year ago

Yes.

BigInt(-3) & BigInt(4)    // 4n

BigNumber(-3).and(4).toString()    // '0'

BigNumber bitiwse operations need to match BigInt.

It's been a while since I looked at this but I remember working on it a bit and getting the results to match but I wasn't happy with the performance due to it using string operations and applying native bitwise ops one bit at a time.

I currently have:

bignumber.zip