Nerdy People Matriculate

    int-bitmask
    TypeScript icon, indicating that this package has built-in type declarations

    0.3.0 • Public • Published

    int-bitmask

    Promotional int-bitmask banner showcasing the functionality of the package Manipulate JavaScript numbers like a bitmask.


    Functions

    checkBit()

    Returns true, false or null depending on checked bit in a given number.

    First arg: number. Second arg: bit index.

    Sample usage:

    checkBit(65, 0); // returns true, as the first bit (index 0) counting from the right is on.
    checkBit(65, 1); // returns false, as the second bit (index 1) counting from the right is off.
    [...]
    checkBit(65, 5); // returns false, as the sixth bit (index 5) counting from the right is off.
    checkBit(65, 6); // returns true, as the seventh bit (index 6) counting from the right is on.
    checkBit(65, 7); // returns null, which is falsy, as the eighth bit (index 7) is not part of the number.
    ---
    checkBit(-1, 0) // throws Error: Inputs cannot be negative.
    checkBit(2.5, 0.5) // throws Error: Inputs must be integers.

    setBit()

    Takes a number, changes the given bit and returns the new number.

    First arg: number. Second arg: bit index. Third arg: new bit state (0 | 1).

    Sample usage:

    setBit(64, 0, 1); // returns 65
    setBit(128, 0, 1); // returns 129
    setBit(64, 6, 0); // returns 0
    ---
    setBit(-1, 0, 1) // throws Error: Inputs cannot be negative.
    setBit(2.5, 0.5, 0) // throws Error: Inputs must be integers.

    toggleBit()

    Takes a number, toggles the given bit and returns the new number.

    First arg: number. Second arg: bit index.

    Sample usage:

    toggleBit(0, 6) // returns 64
    toggleBit(64, 6) // returns 0
    ---
    toggleBit(-1, 0) // throws Error: Inputs cannot be negative.
    toggleBit(2.5, 0.5) // throws Error: Inputs must be integers.

    Keywords

    none

    Install

    npm i int-bitmask

    DownloadsWeekly Downloads

    22

    Version

    0.3.0

    License

    MIT

    Unpacked Size

    5.3 kB

    Total Files

    5

    Last publish

    Collaborators

    • jakubmanczak