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 🙏

© 2025 – Pkg Stats / Ryan Hefner

bitflagsjs

v2.1.1

Published

managing bit flags by javascript and typescript.

Downloads

9

Readme

Main

BitFlagsJs

A lightweight library for easy bit flag management in JavaScript and TypeScript. BitFlagsJs is a simple implementation of a BitArray, providing an efficient way to handle bit flags with a minimal API.

NPM Version License: MIT

Installation

npm install bitflagsjs
# or
yarn add bitflagsjs

Key Features

BitFlagsJs simplifies bit flag operations in JavaScript and TypeScript:

  • Simple BitArray implementation with minimal overhead
  • Manage bit flags using arrays of 32-bit integers
  • Simple API for setting, unsetting, and checking bits
  • Automatic handling of indices beyond 32-bit boundaries
  • First-class TypeScript support with complete type definitions

Usage

Importing

// ES Modules
import BitFlags from 'bitflagsjs';

// CommonJS
// const BitFlags = require('bitflagsjs').default;

TypeScript Usage

import BitFlags from 'bitflagsjs';

// Type-safe bit flags
const flags = new BitFlags();
flags.set(10);
const isSet: boolean = flags.is(10);
const flagArray: number[] = flags.get();

Creating an Instance

// Initialize with default value [0]
const bitFlags = new BitFlags();

// Initialize with custom values
const initializedFlags = new BitFlags([5, 10]); // [0x00000101, 0x00000a00]

Methods

get()

Returns the current bit mask array.

const bitFlags = new BitFlags();
bitFlags.set(2);
bitFlags.set(35);
console.log(bitFlags.get()); // [4, 8]  // [0x00000100, 0x00000008]

is(index)

Checks if the bit at the specified index is set.

const bitFlags = new BitFlags([3]); // [0x00000011]
const isMaskedFirstBit = bitFlags.is(0);  // true
const isMaskedSecondBit = bitFlags.is(1); // true
const isMaskedThirdBit = bitFlags.is(2);  // false

set(index)

Sets the bit at the specified index.

const bitFlags = new BitFlags(); // [0x00000000]
bitFlags.set(0); // [0x00000001]
bitFlags.set(1); // [0x00000011]
bitFlags.set(4); // [0x00001011]

unset(index)

Unsets the bit at the specified index.

const bitFlags = new BitFlags([3]); // [0x00000011]
bitFlags.unset(0); // [0x00000010]
bitFlags.unset(1); // [0x00000000]

clear()

Clears all bit flags, resetting the array to its initial state.

const bitFlags = new BitFlags([15, 7]); // [0x0000000f, 0x00000007]
bitFlags.clear(); // [0x00000000]
console.log(bitFlags.get()); // [0]

count()

Returns the number of bits set in the bit flag array.

const bitFlags = new BitFlags([3]); // [0x00000011] (2 bits set)
console.log(bitFlags.count()); // 2

bitFlags.set(4); // [0x00010011] (3 bits set)
console.log(bitFlags.count()); // 3

Bitwise Operations

BitFlagsJs supports bitwise operations between two BitFlags instances:

and(other)

Returns a new BitFlags instance with the bit flags that are set in both this and the other BitFlags instance.

const flags1 = new BitFlags([5]); // [0x00000101]
const flags2 = new BitFlags([3]); // [0x00000011]
const result = flags1.and(flags2); // [0x00000001]
console.log(result.get()); // [1]
or(other)

Returns a new BitFlags instance with the bit flags that are set in either this or the other BitFlags instance.

const flags1 = new BitFlags([5]); // [0x00000101]
const flags2 = new BitFlags([3]); // [0x00000011]
const result = flags1.or(flags2); // [0x00000111]
console.log(result.get()); // [7]
xor(other)

Returns a new BitFlags instance with the bit flags that are set in either this or the other BitFlags instance, but not in both.

const flags1 = new BitFlags([5]); // [0x00000101]
const flags2 = new BitFlags([3]); // [0x00000011]
const result = flags1.xor(flags2); // [0x00000110]
console.log(result.get()); // [6]
not()

Returns a new BitFlags instance with all bits flipped.

const flags = new BitFlags([5]); // [0x00000101]
const result = flags.not(); // [0xfffffffa]
console.log(result.is(0)); // false
console.log(result.is(1)); // true
console.log(result.is(2)); // true

Example: User Permissions

// Define permissions
const PERMISSIONS = {
  READ: 0,
  WRITE: 1,
  DELETE: 2,
  ADMIN: 3
};

// Set user permissions
const userPermissions = new BitFlags();
userPermissions.set(PERMISSIONS.READ);
userPermissions.set(PERMISSIONS.WRITE);

// Check permissions
if (userPermissions.is(PERMISSIONS.READ)) {
  console.log('User has read permission');
}

if (!userPermissions.is(PERMISSIONS.ADMIN)) {
  console.log('User does not have admin permission');
}

// Remove permissions
userPermissions.unset(PERMISSIONS.WRITE);

Technical Details

BitFlagsJs manages bit flags using arrays of 32-bit integers. When using indices beyond 32, new array elements are automatically allocated.

Contributing

Want to contribute to this project? You're welcome!

  1. Fork this repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'feat: add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Commit Message Convention

  • feat: Add a new feature
  • fix: Fix a bug
  • docs: Update documentation
  • style: Format code, missing semicolons, etc. (no code change)
  • refactor: Refactor code
  • test: Add or update tests
  • chore: Update build process or auxiliary tools

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contact

Note: A Korean version of this document is available at README-KR.md