Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bitfield

Package Overview
Dependencies
Maintainers
3
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitfield

a simple bitfield, compliant with the BitTorrent spec

  • 4.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.4K
increased by7.98%
Maintainers
3
Weekly downloads
 
Created
Source

bitfield

A simple bitfield, compliant with the BitTorrent spec.

npm install bitfield
Example
import Bitfield from "bitfield";

const field = new Bitfield(256); // Create a bitfield with 256 bits.

field.set(128); // Set the 128th bit.
field.set(128, true); // Same as above.

field.get(128); // `true`
field.get(200); // `false` (all values are initialised to `false`)
field.get(1e3); // `false` (out-of-bounds is also false)

field.set(128, false); // Set the 128th bit to 0 again.

field.buffer; // The buffer used by the bitfield.

Class: BitField

Constructors

  • constructor

Properties

Methods

Constructors

constructor

+ new BitField(data?: number | Uint8Array, opts?: BitFieldOptions): BitField

Parameters:
NameTypeDefault valueDescription
datanumber | Uint8Array0Either a number representing the maximum number of supported bytes, or a Uint8Array.
opts?{ grow: number }{ grow: 0 }

grow:

If you set an index that is out-of-bounds, the bitfield will automatically grow so that the bitfield is big enough to contain the given index, up to the given size (in bit).

If you want the Bitfield to grow indefinitely, pass Infinity.

Returns: BitField

Properties

buffer

buffer: Uint8Array

The internal storage of the bitfield.

Methods

forEach

forEach(fn: (bit: boolean, index: number) => void, start?: number, end?: number): void

Loop through the bits in the bitfield.

Parameters:
NameTypeDefault valueDescription
fn(bit: boolean, index: number) => void-Function to be called with the bit value and index.
startnumber0Index of the first bit to look at.
endnumberthis.buffer.length * 8Index of the first bit that should no longer be considered.

Returns: void


get

get(i: number): boolean

Get a particular bit.

Parameters:
NameTypeDescription
inumberBit index to retrieve.

Returns: boolean

A boolean indicating whether the ith bit is set.


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.

Parameters:
NameTypeDefault valueDescription
inumber-Bit index to set.
valuebooleantrueValue to set the bit to. Defaults to true.

Returns: void


setAll

setAll(array: ArrayLike<boolean>, offset?: number): void

Set the bits in the bitfield to the values in the given array.

Parameters:
NameTypeDefault valueDescription
arrayArrayLike<boolean>-Array of booleans to set the bits to.
offsetnumber0Index of the first bit to set.

Returns: void

License

MIT

Keywords

FAQs

Package last updated on 05 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc