npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bitwise53

v1.0.2

Published

Bitwise operations for integers up to 53 bits

Downloads

17

Readme

Bitwise53

Bitwise operations for integers up to 53 bits

Description

Bitwise operations in javascript are limited to 32 bit integers. Any excess bits are simply cut off which makes bitwise operations impossible for any number bigger than 32 bits despite javascript supporting integers with precision up to 53 bits.

The typical solution to this problem is to use BigInt, which supports bitwise operations of any bit length. However, BigInts are notoriously slow, up to 50x slower than regular numbers.

This library provides functions to correctly perform bitwise opeations on numbers up to 53 bits without using BigInt, which keeps performance on pair with their 32 bit counterparts.

Usage

npm install bitwise53

const { and, or, xor, not, rshift, lshift, ushift } = require("bitwise53")
// or import { and, or, xor, not, rshift, lshift, ushift } from "bitwise53"

and(a, b) // a & b
or(a, b) // a | b
xor(a, b) // a ^ b
not(a) // ~a
rshift(a, 5) // a >> 5
lshift(a, 5) // a << 5
ushift(a, 5) // a >>> 5

Performance

npm install -D bitwise53
npm run bench

Number x 738,481,279 ops/sec ±0.48% (90 runs sampled)
Bitwise53 x 730,536,122 ops/sec ±0.32% (96 runs sampled)
BigInt x 19,083,679 ops/sec ±1.58% (89 runs sampled)

Notes

There are some minor differences in behaviour compared to regular bitwise operators when dealing with some negative numbers:

When shifting a negative number of bits, the right-hand operand overflows and loops around, for example n >> -5 would result in n >> 27 (32-5). BigInts on the other hand reverse the operation because they dont have a fixed size to overflow, so n >> -5 would result in n << 5. This library follows the BigInt approach and reverses the operation:

555 >> -2 // 0
555n >> -2n // 2220n
rshift(555, -2) // 2220

The unsigned right shift function ushift assumes a 53 bit size by default, therefore it will produce different results from the >>> operator when shifting negative numbers. This function accepts a third parameter to specify the desired bit size to correctly place the sign bit. Setting it to 32 for example will produce the same results as the >>> operator:

ushift(-89375, 0)        // 11111111111111111111111111111111111101010001011100001
ushift(-89375, 0, 24)    // 111111101010001011100001
ushift(-89375, 0, 32)    // 11111111111111101010001011100001
-89375 >>> 0             // 11111111111111101010001011100001