What is bit-twiddle?
The bit-twiddle npm package provides a collection of functions for performing common bitwise operations in JavaScript. It is useful for tasks that require low-level manipulation of binary data, such as graphics programming, cryptography, and performance optimization.
What are bit-twiddle's main functionalities?
Set a bit
This feature allows you to set a specific bit in a number. In the example, the second bit (index 1) of the binary number 0000 is set, resulting in 0010.
const bt = require('bit-twiddle');
let num = 0b0000;
num = bt.setBit(num, 1);
console.log(num.toString(2)); // Output: 10
Clear a bit
This feature allows you to clear a specific bit in a number. In the example, the third bit (index 2) of the binary number 1111 is cleared, resulting in 1011.
const bt = require('bit-twiddle');
let num = 0b1111;
num = bt.clearBit(num, 2);
console.log(num.toString(2)); // Output: 1011
Toggle a bit
This feature allows you to toggle a specific bit in a number. In the example, the second bit (index 1) of the binary number 1010 is toggled, resulting in 1000.
const bt = require('bit-twiddle');
let num = 0b1010;
num = bt.toggleBit(num, 1);
console.log(num.toString(2)); // Output: 1000
Check if a bit is set
This feature allows you to check if a specific bit in a number is set. In the example, the second bit (index 1) of the binary number 1010 is checked, and it is set.
const bt = require('bit-twiddle');
let num = 0b1010;
let isSet = bt.isBitSet(num, 1);
console.log(isSet); // Output: true
Count the number of set bits
This feature allows you to count the number of set bits (1s) in a number. In the example, the binary number 1011 has three set bits.
const bt = require('bit-twiddle');
let num = 0b1011;
let count = bt.popCount(num);
console.log(count); // Output: 3
Other packages similar to bit-twiddle
bitwise
The bitwise package provides a comprehensive set of bitwise operations for JavaScript. It includes functions for bit manipulation, bitwise arithmetic, and more. Compared to bit-twiddle, bitwise offers a broader range of functionalities and is more actively maintained.
bitset
The bitset package allows for the manipulation of large sets of bits. It provides a BitSet class that supports various bitwise operations. While bitset is more focused on handling large bitsets efficiently, bit-twiddle is more about providing simple, low-level bitwise operations.
bitwise-operator
The bitwise-operator package offers a collection of bitwise operations similar to bit-twiddle. It includes functions for setting, clearing, toggling, and checking bits. However, bitwise-operator is less popular and less frequently updated compared to bit-twiddle.
bit-twiddle
This is a collection of miscellaneous bit twiddling hacks ported to JavaScript, mostly taken from here:
Install
Via npm:
npm install bit-twiddle
API
sign(v)
Computes the sign of the integer v. Returns:
- -1 if v < 0
- 0 if v === 0
- +1 if v > 0
abs(v)
Returns the absolute value of the integer v
min(x,y)
Computes the minimum of integers x and y
max(x,y)
Computes the maximum of integers x and y
isPow2(v)
Returns true
if v is a power of 2, otherwise false.
log2(v)
Returns an integer approximation of the log-base 2 of v
log10(v)
Returns log base 10 of v.
popCount(v)
Counts the number of bits set in v
countTrailingZeros(v)
Counts the number of trailing zeros.
nextPow2(v)
Rounds v up to the next power of 2.
prevPow2(v)
Rounds v down to the previous power of 2.
parity(v)
Computes the parity of the bits in v.
reverse(v)
Reverses the bits of v.
interleave2(x,y)
Interleaves a pair of 16 bit integers. Useful for fast quadtree style indexing. (See wiki: http://en.wikipedia.org/wiki/Z-order_curve )
deinterleave2(v, n)
Deinterleaves the bits of v, returns the nth part. If both x and y are 16 bit, then it is true that:
deinterleave2(interleave2(x,y), 0) === x
deinterleave2(interleave2(x,y), 1) === y
interleave3(x,y,z)
Interleaves a triple of 10 bit integers. Useful for fast octree indexing.
deinterleave3(v, n)
Same deal as deinterleave2
, only for triples instead of pairs
nextCombination(x)
Returns next combination ordered colexicographically.
Acknowledgements
Code is ported from Sean Eron Anderson's public domain bit twiddling hacks page. http://graphics.stanford.edu/~seander/bithacks.html
JavaScript implementation (c) 2013 Mikola Lysenko. MIT License