on-internet
← Community

How to obtain the number of bits (bit length) of a JavaScript BigInt?

Stack Overflow ·

There is no bitLength() method documented in the MDN BigInt reference or the JSBI repository. How can I find the number of bits (the bit length) of a native BigInt value? I am looking for a "direct way", using some internal information, or anything faster than doing x.toString(2). Not ideal, but an interim workaround (seems faster than toString()) is to use this mathematical formula based on log2(x) adapted for BigInt. This library implements a specific integerLogarithm(value, base), which could be used to calculate log2 and implement a bitLength() method (without Math.floor()), but it has a l

Comments

Sign in or sign up to comment and upvote.

No comments yet.