Napoleonic Political Magnificence

    This package has been deprecated

    Author message:

    Please use BitSet.js (

    DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/bit-array package

    0.2.4 • Public • Published

    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.


    The BitArray module has two constructors:

    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:

    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>
    <dd>Returns the value of the bit at index (boolean.)</dd>
    <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>
    <dd>Resets the BitArray so that it is empty and can be re-used.</dd>
    <dd>Returns a copy of this BitArray.</dd>
    <dd>Returns true if this BitArray equals another. Two BitArrays are considered equal if both have the same length and bit pattern.</dd>
    <dd>Returns the JSON representation of this BitArray.</dd>
    <dd>Returns a string representation of the BitArray with bits in logical order.</dd>
    <dd>Returns a hex representation of the BitArray.</dd>
    <dd>Convert the BitArray to an Array of boolean values.</dd>
    <dd>Returns the total number of bits set to 1 in this BitArray.</dd>
    <dd>Inverts this BitArray.</dd>
    <dd>Bitwise OR on the values of this BitArray using BitArray `other`.</dd>
    <dd>Bitwise AND on the values of this BitArray using BitArray `other`.</dd>
    <dd>Bitwise XOR on the values of this BitArray using BitArray `other`.</dd>


    You can install the bit array module using npm:

    > npm install bit-array

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


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

    Ports - PHP port by Pavel Ptacek


    npm i bit-array

    DownloadsWeekly Downloads






    Unpacked Size

    22.5 kB

    Total Files


    Last publish


    • bramstein