Code Monkey home page Code Monkey logo

bit-array's Introduction

Bit-Array is deprecated.

Please use BitSet.js

JavaScript Bit Array Library

This library contains a JavaScript implementation of bit arrays. The library supports:

  • getting, setting and toggling of individual bits
  • iterating over each bit
  • counting the number of "on" bits
  • bitwise operations with other bit arrays such as OR, AND and XOR.
  • serialization to and from JSON
  • Browser, Node.js and Ender.js compatible

The bit array is sparse. The following example shows how to set and get individual bits within the array:

a = new BitArray(32);
a.set(0, true);
a.set(31, true);
a.toString(); // "10000000000000000000000000000001"
a.get(1); // false
a.get(31); // true

Note that the array internally uses 32 bit integers (actually, JavaScript's number type is 64 bit, but only 32 bits can be addressed using bitwise operations,) so going beyond the given length throws an error:

a.set(32, true); // throws an index of range exception

Even though bit arrays are not that useful in JavaScript, there is one place where they excel; encoding large boolean sets for transfer between the browser and server. A JSON representation of a bit array is much smaller than an actual boolean array.

API

The BitArray module has two constructors:

BitArray(size)
Creates a new empty bit array with the given size in bits.
<dt>BitArray(size, hex)</dt>
<dd>Creates a new bit array with the given size and using the hex string as value</dd>

The following instance methods are supported:

size()
Returns the total number of bits in the BitArray.
<dt>set(index, boolean)</dt>
<dd>Sets the bit at index to a value (boolean.)</dd>

<dt>get(index)</dt>
<dd>Returns the value of the bit at index (boolean.)</dd>

<dt>toggle(index)</dt>
<dd>Toggles the bit at index. If the bit is on, it is turned off. Likewise, if the bit is off it is turned on.</dd>

<dt>reset()</dt>
<dd>Resets the BitArray so that it is empty and can be re-used.</dd>

<dt>copy()</dt>
<dd>Returns a copy of this BitArray.</dd>

<dt>equals(other)</dt>
<dd>Returns true if this BitArray equals another. Two BitArrays are considered equal if both have the same length and bit pattern.</dd>

<dt>toJSON()</dt>
<dd>Returns the JSON representation of this BitArray.</dd>

<dt>toString()</dt>
<dd>Returns a string representation of the BitArray with bits in logical order.</dd>

<dt>toHexString()</dt>
<dd>Returns a hex representation of the BitArray.</dd>

<dt>toArray()</dt>
<dd>Convert the BitArray to an Array of boolean values.</dd>

<dt>count()</dt>
<dd>Returns the total number of bits set to 1 in this BitArray.</dd>

<dt>not()</dt>
<dd>Inverts this BitArray.</dd>

<dt>or(other)</dt>
<dd>Bitwise OR on the values of this BitArray using BitArray `other`.</dd>

<dt>and(other)</dt>
<dd>Bitwise AND on the values of this BitArray using BitArray `other`.</dd>

<dt>xor(other)</dt>
<dd>Bitwise XOR on the values of this BitArray using BitArray `other`.</dd>

Installation

You can install the bit array module using npm:

> npm install bit-array

Alternatively you could just include bit-array.js in your project.

License

Licensed under the revised BSD License. Copyright 2010-2012 Bram Stein. All rights reserved.

Ports

https://github.com/foglcz/bit-array - PHP port by Pavel Ptacek

bit-array's People

Contributors

bramstein avatar infusion avatar jscharlach avatar mattbornski avatar senderista avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bit-array's Issues

Binary and Hex Values Disagree

Using this function to convert a binary string to hex, I wrote this code to compare the output of toHexString(), toString(), and toBinaryString():

var bits = new BitArray(256);
[
    4, 6, 32, 53, 67, 89, 104, 126, 157, 179, 190, 201, 212, 213, 227, 238, 242
].forEach( function (i) {
    bits.set(i, true);
});
console.log('Bits:    ' + bits.toString());
console.log('Binary:  ' + bits.toBinaryString());
console.log('Hex:     ' + bits.toHexString());
console.log('BitsHex: ' + binaryToHex(bits.toString()));
console.log('BinHex:  ' + binaryToHex(bits.toBinaryString()));

The output was:

Bits:    0000101000000000000000000000000010000000000000000000010000000000000100000000000000000000010000000000000010000000000000000000001000000000000000000000000000000100000000000000000000010000000000100000000001000000000011000000000000010000000000100010000000000000
Binary:  0000000000000100010000000000100000000000001100000000001000000000010000000000100000000000000000000010000000000000000000000000000001000000000000000000000100000000000000100000000000000000000010000000000000100000000000000000000100000000000000000000000001010000
Hex:     0000005000200001020000084000010020000000400800000030020000044008
BitsHex: 0A000000800004001000004000800002000000040000100200400C0010022000
BinHex:  0004400800300200400800002000000040000100020000080020000100000050

The curious thing is that the Hex output matches neither the BitHex nor the BinHex output. I plugged the Bits and Binary strings into a quick Perl script to see what hex would be generated, and they agreed with the output of BitsHex and BinHex. So I don't think that toHexString() is outputting bytes in the correct order. I suspect it should output them in the same order as toString() does.

.equals() always returning true

I am trying to check if two bitarray are equivelant, but I get 'true' no matter what. Let's say I have the following bitarrays:

bitarray_1.toString() =  001001000
bitarray_2.toString() =  101001000

bitarray_1.equals(bitarray_2) // this should return false
-> true

Maybe i made a mistake

But if i make something like :

var a = new BitArray(10);
a.set(0, true);
a.not();

console.log(a.count());

I got 31 as result instead of 9 is it ok ?

【bug】size is no use in new BitArray(size, hex)

see code in line 18 in https://github.com/bramstein/bit-array/blob/master/lib/bit-array.js

if (hex.length * 4 > this.length) {
    throw 'Hex value is too large for this bit array.'
} else if (hex.length * 4 < this.length) {
    // pad it
    while(hex.length * 4 < this.length) {
        hex = '0' + hex;
    }
}

what this.length is about? is it undefined?
well, this causes size do nothing when new BitArray(size, hex)

test in chrome, or maybe this is only for nodejs module?

[BUG] Count is not accurate.

Running a binary progam, and noticed that COUNT is not accurate.
this is my code:
console.log(possible.toString()+" count is: "+possible.count()+" and i is:"+i);
and this is the output:
011111111 count is: 31 and i is:8
you see that there are only 8 ones and still it says 31.

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.