MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / bitLength

Function bitLength

src/big-decimal/utils.ts:47–62  ·  view source on GitHub ↗
(n: bigint)

Source from the content-addressed store, hash-verified

45
46/** Bit length of |n| (the number of bits in its binary representation). */
47export function bitLength(n: bigint): number {
48 if (n < 0n) n = -n;
49 if (n === 0n) return 0;
50 let bits = 0;
51 // Doubling search to bracket the bit length.
52 let high = 1;
53 while (n >> BigInt(high) > 0n) high *= 2;
54 // Binary search within [0, high].
55 for (let shift = high >> 1; shift >= 1; shift >>= 1) {
56 if (n >> BigInt(shift) > 0n) {
57 bits += shift;
58 n >>= BigInt(shift);
59 }
60 }
61 return bits + 1;
62}
63
64/** Fixed-point multiply on the base-2 grid: (a * b) >> bits */
65export function fpmul(a: bigint, b: bigint, bits: number): bigint {

Callers 9

utils.test.tsFile · 0.90
fromFixedPointFunction · 0.90
cbrtSeedFunction · 0.90
fpsqrtFunction · 0.85
bigSqrtSeedFunction · 0.85
fpexpFunction · 0.85
fplnAGMFunction · 0.85
bigintSqrtFunction · 0.85
fpsincosFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected