UNPKG

506 BJavaScriptView Raw
1'use strict'
2/**
3 * Bitwise not
4 * @param {BigNumber} x
5 * @return {BigNumber} Result of ~`x`, fully precise
6 *
7 */
8module.exports = function bitNot (x) {
9 if (x.isFinite() && !x.isInteger()) {
10 throw new Error('Integer expected in function bitNot')
11 }
12
13 const BigNumber = x.constructor
14 const prevPrec = BigNumber.precision
15 BigNumber.config({ precision: 1E9 })
16
17 let result = x.plus(new BigNumber(1))
18 result.s = -result.s || null
19
20 BigNumber.config({ precision: prevPrec })
21 return result
22}