Giter Site home page Giter Site logo

wbitmask's Introduction

wBitmask Build Status

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

npm install
node sample/Sample.js

Usage

var _ = 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' + _.toStr( 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' + _.toStr( restoredMap ) );
/*restoredMap :
{
  hidden : true,
  system : false,
  terminal : false,
  directory : true,
  link : true
}*/

wbitmask's People

Contributors

wandalen avatar vmihdal avatar dmvict avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.