The `bit-twiddle` package provides a highly optimized collection of fundamental bit manipulation functions, ported primarily from Stanford's extensive "Bit Twiddling Hacks" guide. It offers utilities for common bitwise operations such as computing the sign of an integer, absolute value, checking if a number is a power of two, logarithmic approximations (`log2`, `log10`), population count (`popCount`), and finding the next or previous power of two. Additionally, it includes specialized functions for interleaving and deinterleaving bits (`interleave2`, `interleave3`), crucial for efficient spatial indexing schemes like quadtrees and octrees (Z-order curves). The current stable version is 1.0.2, published in 2014. Due to the static nature of bitwise algorithms and the package's age, it is considered abandoned, with no active development or planned release cadence. Its primary differentiator remains its direct, performant JavaScript implementations of these classic algorithms, making it valuable for performance-critical numerical computing or graphics applications where low-level bit manipulation is essential.
npm install bit-twiddleVerified import paths — ran on the pinned version, not inferred.
Demonstrates several core bit-twiddle functions including sign, power-of-2 checks, population count, and 2D bit interleaving/deinterleaving for spatial indexing.
Be aware of JavaScript's 32-bit integer limits for bitwise operations. If working with numbers beyond this range, consider alternative libraries or custom implementations that handle larger integer types.
Do not use `log2` or `log10` for precise mathematical calculations. For exact logarithms, use `Math.log2()` or `Math.log10()` (or `Math.log(v) / Math.log(base)`).
For new projects, consider using a more actively maintained bitwise utility library (e.g., `bitwise` or custom implementations) or carefully evaluate if the performance benefits of this specific library outweigh the risks of using unmaintained code. Always thoroughly test performance critical sections.
For CommonJS, use `const { sign } = require('bit-twiddle');` or `const bitTwiddle = require('bit-twiddle'); const sign = bitTwiddle.sign;`Use ESM import syntax: `import { sign } from 'bit-twiddle';` Ensure your environment correctly handles ESM modules (e.g., Node.js v12+ or a bundler).No dependency data recorded yet.