/Bits-js

A simple and fast bit set implementation in Javascript

Primary LanguageJavaScript

Bits

A fast bit set (aka bit array) implementation in Javascript by using an array of integers to store the state. Besides set and test primitives it allows to perform bitwise operations between bitsets.

The memory consumption should be just above the double of the number of bits stored in the set, since JS casts to 32bit integers before doing bitwise operations even if the internal numeric type is always 64bit long.

It should be compatible with most Javascript environments be it in the browser or in the server.

Example

// Create a new bit set
var bs = new Bits();

bs.set(100);
if (bs.test(100)) {
    alert('Bit 100 was set!');
}

// Clone the bit set
var bs2 = bs.clone();

// Apply XOR on the first set against the values in the second
bs.xor(bs2);
if (bs.test(100)) {
    alert('Bit 100 is still set but it should not!');
}

Using in Node

// Import the library with require
var Bits = require('./path/to/Bits.js').Bits;

More information

The library is dead simple, just check out the source code to find out more ;)

License

The MIT License

Copyright (c) 2011 Iván -DrSlump- Montes

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.