#JavaScript is a weird language; it has #bigint, but it doesn't have a function that returns a number of bits in the bigint. Neither it has #log2 function for the bigints. If you need a workaround for the problem, see the article https://medium.com/@sergeyshandar/improving-the-log2-algorithm-for-bigint-in-javascript-7e5692e7bf0b?sk=9ac18367f61b524704cc8d82707aed3d
Improving the Log2 Algorithm for BigInt in JavaScript

I usually use the bigint type in JavaScript and FunctionalScript as a vector of bits, and I often need to know how many bits are in a specific bigint instance. Every JavaScript engine that has a…

Medium

Does anyone know how to make a fast log2 function for bigint in JavaScript?

I know that JS VM can do it in almost O(1) because it has information about the size of bigint. My current best algorithm is O(log(n)). The algorithm performs faster than x.toString(2).length and consumes less memory. Do you have any ideas for improvement?

https://github.com/functionalscript/functionalscript/blob/a9a1cd2c1b0f4c025faf2223eb6ff96f48e8374b/types/bigint/module.f.cjs#L65

#JavaScript #bigint #log2 #Algorithm

functionalscript/types/bigint/module.f.cjs at a9a1cd2c1b0f4c025faf2223eb6ff96f48e8374b · functionalscript/functionalscript

FunctionalScript is a purely functional subset of JavaScript - functionalscript/functionalscript

GitHub