Bit Twiddling Hacks

1.0.2 · abandoned · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates several core bit-twiddle functions including sign, power-of-2 checks, population count, and 2D bit interleaving/deinterleaving for spatial indexing.

import { sign, isPow2, nextPow2, popCount, interleave2, deinterleave2 } from 'bit-twiddle';

// Basic bitwise operations
console.log('Sign of -5:', sign(-5)); // Expected: -1
console.log('Is 16 a power of 2?', isPow2(16)); // Expected: true
console.log('Is 15 a power of 2?', isPow2(15)); // Expected: false
console.log('Next power of 2 for 27:', nextPow2(27)); // Expected: 32
console.log('Number of set bits in 42 (00101010b):', popCount(42)); // Expected: 3

// Z-order curve (quadtree/octree) indexing
const x = 12;
const y = 25;
const interleaved = interleave2(x, y);
console.log(`Interleaving (x=${x}, y=${y}): ${interleaved} (binary: ${interleaved.toString(2)})`);

const deinterleavedX = deinterleave2(interleaved, 0);
const deinterleavedY = deinterleave2(interleaved, 1);
console.log(`Deinterleaving: x=${deinterleavedX}, y=${deinterleavedY}`);

// Demonstrating 32-bit integer limits (JavaScript bitwise operations clamp to 32-bit)
const largeNum = 2 ** 31 - 1; // Max signed 32-bit int
console.log(`PopCount for max signed 32-bit int (${largeNum}):`, popCount(largeNum));

const exceedingNum = 2 ** 32; // Exceeds signed 32-bit int range in bitwise ops
console.log(`Is 2^32 a power of 2 (conceptually)? ${isPow2(exceedingNum)}. Actual bitwise value for isPow2 is based on 32-bit interpretation.`, isPow2(exceedingNum));

view raw JSON →