Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
bit-twiddle
Advanced tools
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.
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
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.
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.
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.
This is a collection of miscellaneous bit twiddling hacks ported to JavaScript, mostly taken from here:
Via npm:
npm install bit-twiddle
sign(v)
Computes the sign of the integer v. Returns:
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.
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
FAQs
Bit twiddling hacks for JavaScript
The npm package bit-twiddle receives a total of 57,615 weekly downloads. As such, bit-twiddle popularity was classified as popular.
We found that bit-twiddle demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.