GithubHelp home page GithubHelp logo

robertcasanova / bitset.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from infusion/bitset.js

0.0 1.0 0.0 316 KB

A Bit-Vector implementation in JavaScript

Home Page: http://www.xarg.org/2014/03/javascript-bit-array/

JavaScript 100.00%

bitset.js's Introduction

BitSet.js

BitSet.js is a Bit-Array implementation in JavaScript.

With this library, you can work on large bit vectors without worring about system constraints, given by integer size.

Examples

Basic usage

var bs = new BitSet(500); // We need 500 bits
bs.set(128, 1); // Set bit at position 128

If you need a sanity check on your own, you could write something like:

if (bs.set(n, 1) !== null) {
   // Success
}

or

if (0 <= n && n < bs.size) {
   bs.set(n, 1);
   // Success
}

Default Value Set

var bs = new BitSet(2, 1); // Set all bits initially to 1

Range Set

var bs = new BitSet; // Default is 31 bit
bs.setRange(10, 18, 1); // Set a 1 between 10 and 18

Flipping bits

var bs = new BitSet(40);
bs.flip();
bs.flip(29, 35);
var str = bs.toString("-"); // Separator every 31 bits is the dash "-"

if (str === "1111111111111111111111111100000-0011111111111111111111111111111") {
   console.log("YES!");
}

Installation

npm install bitset.js

or

bower install bitset.js

Available methods

Bitwise functions

Bitwise not

BitSet not(BitSet x)

Bitwise and

BitSet and(BitSet x)

Bitwise or

BitSet or(BitSet x)

Bitwise nand

BitSet nand(BitSet x)

Bitwise nor

BitSet nor(BitSet x)

Bitwise xor

BitSet xor(BitSet x)

Set a bit at position index, default 1

BitSet set(int index, int value=1)

Get a bit at position index

BitSet get(int index)

Set a range of bits, either by a binary string or by a single bit value

BitSet setRange(int start, int end, String binstr)
BitSet setRange(int start, int end, int value)

Retrieve a range of bits, indicated by start and end index

BitSet getRange(int start, int end)

Get the number of bits set

int cardinality()

Get the most significant bit set, same as log base two

int msb()

Clear a range of bits, either all, a certain position or indicated with start and end

BitSet clear()
BitSet clear(int pos)
BitSet clear(int start, int end)

Invert/Flip either all bits, a single bit position or a range of bits

BitSet flip()
BitSet flip(int pos)
BitSet flip(int start, int end)

Comparision functions

Compare (=same size and all bits equal) two BitSet objects

boolean equals(BitSet x)

Check if all bits of a BitSet are set to 0

boolean isEmpty()

Check if one BitSet is subset of another

boolean subsetOf(Bitset x)

Misc functions

Overrides the toString function for a pretty representation

String toString(String separator="")

Create a 100% copy of the actual BitSet object

BitSet clone()

Coding Style

As every library I publish, bitset.js is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.

Run a test

Testing the source against the shipped test suite is as easy as

npm test

Note

The allocated size is always a power of two minus 1! If you need 500 bits, the actual size allocated is 527 (= ceil(500 / 31)). All the additional bits are usable of course.

If you want to extend the library, please provide test cases in your commit.

bitset.js's People

Contributors

infusion avatar tdzienniak avatar tobyberster avatar

Watchers

Robert Casanova 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.