wBitmask

0.4.356 • Public • Published

module::Bitmask status experimental

A small class to convert a map of Booleans to Integer and vice versa with help of defined schema. The constructor of Bitmask expects names which created instance use for conversion. Use the module to solve the bitmask conversion problem robustly.

Bitmask

Bitmask is a sequence of bits that we can use to store different data in a single value. For example, we can store boolean options flags from map in a single integer. More about bitmask.

By using bitwise operations we can easily get needed bit(s) and set to or compare with value of our variable. For example, by using shift operators and bitwise AND we can get boolean value of our flag masked in a number and set it to our property in a map. More about bitwise operations.

Installation

npm install wBitmask

Try out from the repository

git clone https://github.com/Wandalen/wBitmask
cd wBitmask
will .npm.install
node sample/trivial/Sample.s

Make sure you have utility willbe installed. To install willbe: npm i -g willbe@stable. Willbe is required to build of the module.

To add to your project

npm add 'wBitmask@stable'

Willbe is not required to use the module in your project as submodule.

Usage

let _ = wTools;

/*define array of possible names and bit values, that can vary*/
var defaultFieldsArray =
[

  { hidden : false },
  { system : false },
  { terminal : true },
  { directory : false },
  { link : true },

];

/*create new instance of wBitmask by passing options object with array( defaultFieldsArray ) to the constructor*/
var bitmask = wBitmask
({
  defaultFieldsArray : defaultFieldsArray
});

console.log( 'bitmask' )
console.log( bitmask.toStr() );
/*
bitmask
{
  hidden : false,
  system : false,
  terminal : true,
  directory : false,
  link : true
}
*/

/*define our boolean map*/
var originalMap =
{
  hidden : 1,
  terminal : 0,
  directory : 1,
}

console.log( 'originalMap :\n' + _.entity.exportString( originalMap ) );
/*originalMap :
{ hidden : 1, terminal : 0, directory : 1 }*/

/*Convert boolean map into  32-bit number bitmask*/
var word = bitmask.mapToWord( originalMap );

console.log( 'word : ' + word );
/*word : 25*/

/*Convert 32-bit number bitmask into boolean map */
var restoredMap = bitmask.wordToMap( word );

console.log( 'restoredMap :\n' + _.entity.exportString( restoredMap ) );
/*restoredMap :
{
  hidden : true,
  system : false,
  terminal : false,
  directory : true,
  link : true
}*/

Package Sidebar

Install

npm i wBitmask

Weekly Downloads

19

Version

0.4.356

License

MIT

Unpacked Size

52.1 kB

Total Files

7

Last publish

Collaborators

  • wandalen