postmanlabs/generic-bitmask

Name: generic-bitmask

Owner: Postman

Description: Module to compute and manipulate bitmasks in JavaScript

Created: 2015-09-08 11:45:42.0

Updated: 2018-01-26 12:23:37.0

Pushed: 2018-01-18 00:49:11.0

Homepage: null

Size: 34

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Bitmask

This module enables easy manipulation and usage of bitmasking technique in JavaScript.

What is bitmask?

A bit mask is a binary number or a bitmap where the desired bit(s) are one and the remaining 0. By performing a bitwise AND operation of a value with a bitmask, one can test for certain bits being on.

Using a bitmask, it is very easy to store the boolean status of multiple flags in a single variable (of higher base.) A very common use case is to store “permission” value for an object. For example, whether an entity has read or write permission to an object can be determined easily by storing two flags “read” and “write” within a single variable.

More details on this detailed Wikipedia article on Bitmasking.

Installation

Installation can be done using npm for using in NodeJS applications.

install generic-bitmask --save-dev;
Usage
Bitmask = require('generic-bitmask').Mask;

reate a new instance of bitmask
mask = new Bitmask();

et the mask flags for the mask
.add(3); // sets 3rd bit to positive
.add(5); // sets 5th bit to positive

heck which bit is set to true
ole.log(mask.test(3)); // logs true;
ole.log(mask.test(4)); // logs false;

emove a bit that has been set as true
.remove(5);
ole.log(mask.test(5)); // logs false;

et the raw state of the mask in decimal value
ole.log(mask.get()); // logs 8;

et the raw state of the mask. Useful to load saved mask state
.set(40); // sets 3 and 5 to true;
ole.log(mask.test(3)); // logs true;
ole.log(mask.test(4)); // logs false;
ole.log(mask.test(5)); // logs true;
ole.log(mask.test([3, 5])); // logs true;
Using the mask descriptor
Bitmask = require('generic-bitmask').Mask,
Descriptor = require('generic-bitmask').Descriptor;

reate a new instance of bitmask
mask = new Bitmask(),
descriptors = new Descriptor({
    read: 1,
    write: 2
});

et the mask flags for the mask
riptor.add('read', mask);
riptor.validate('read', mask); // returns true
riptor.extract(mask); // returns ['read']
riptor.remove('read', mask);

heck if bit names are set
riptor.defined('read') // returns true

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.