Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bit-twiddle

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bit-twiddle

Bit twiddling hacks for JavaScript

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

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

Keywords

FAQs

Package last updated on 28 May 2014

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc