• Version 4.1.0
  • Published
  • 22.1 kB
  • No dependencies
  • MIT license


npm i bitfield
yarn add bitfield
pnpm add bitfield


a simple bitfield, compliant with the BitTorrent spec



class BitField

class BitField {}


    constructor(data?: number | Uint8Array, opts?: BitFieldOptions);
    • Parameter data

      Either a number representing the maximum number of supported bytes, or a Uint8Array.

      Parameter opts

      Options for the bitfield.

    property buffer

    buffer: Uint8Array;
    • The internal storage of the bitfield.

    method forEach

    forEach: (
    fn: (bit: boolean, index: number) => void,
    start?: number,
    end?: number
    ) => void;
    • Loop through the bits in the bitfield.

      Parameter fn

      Function to be called with the bit value and index.

      Parameter start

      Index of the first bit to look at.

      Parameter end

      Index of the first bit that should no longer be considered.

    method get

    get: (i: number) => boolean;
    • Get a particular bit.

      Parameter i

      Bit index to retrieve.


      A boolean indicating whether the ith bit is set.

    method set

    set: (i: number, value?: boolean) => void;
    • Set a particular bit.

      Will grow the underlying array if the bit is out of bounds and the grow option is set.

      Parameter i

      Bit index to set.

      Parameter value

      Value to set the bit to. Defaults to true.

    Package Files (1)

    Dependencies (0)

    No dependencies.

    Dev Dependencies (11)

    Peer Dependencies (0)

    No peer dependencies.


    To add a badge like this badgeto your package's README, use the codes available below.

    You may also use to create a custom badge linking to

    • Markdown
    • HTML
      <a href=""><img src="" alt=""></a>