# bitfield

> a simple bitfield, compliant with the BitTorrent spec

Latest version **5.0.1** (published 2026-03-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install bitfield
pnpm add bitfield
yarn add bitfield
bun add bitfield
```

## Health

**Score 70/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.0.1 |
| Published | 2026-03-17 |
| First published | 2012-11-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20.19.0 |
| Dependencies | 0 |
| Unpacked size | 24.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 88 |
| Author | Felix Boehm |
| Maintainers | fb55, mafintosh, feross |
| Keywords | bitfield, buffer, bittorrent |

## Links

- npm: https://www.npmjs.com/package/bitfield
- Repository: https://github.com/fb55/bitfield
- Homepage: https://github.com/fb55/bitfield#readme
- Issues: https://github.com/fb55/bitfield/issues
- Funding: https://github.com/sponsors/fb55
- npm.io page: https://npm.io/package/bitfield

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 5.0.1 (latest) — 2026-03-17
- 5.0.0 — 2026-03-17
- 4.2.0 — 2024-01-05
- 4.1.0 — 2022-04-22
- 4.0.0 — 2020-10-30
- 3.0.0 — 2019-07-10
- 2.0.0 — 2018-02-20
- 1.1.2 — 2015-07-03
- 1.1.1 — 2015-02-16
- 1.1.0 — 2015-02-05
- 1.0.2 — 2014-05-07
- 1.0.1 — 2014-04-10
- 1.0.0 — 2014-04-10
- 0.3.0 — 2014-03-23
- 0.2.0 — 2014-03-12
- … 3 more at https://npm.io/package/bitfield/versions

## README

# bitfield

A simple bitfield, compliant with the BitTorrent spec.

    npm install bitfield

#### Example

```js
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](#constructor)

### Properties

- [buffer](#buffer)

### Methods

- [forEach](#foreach)
- [get](#get)
- [set](#set)

## Constructors

### constructor

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

#### Parameters:

| Name    | Type                 | Default value | Description                                                                                                                                                                                                                                                       |
| ------- | -------------------- | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `data`  | number \| Uint8Array | 0             | Either a number representing the maximum number of supported bytes, or a Uint8Array.                                                                                                                                                                              |
| `opts?` | { grow: number }     | { grow: 0 }   | <p>**grow:**<p>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). <p>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:

| Name    | Type                                  | Default value           | Description                                                 |
| ------- | ------------------------------------- | ----------------------- | ----------------------------------------------------------- |
| `fn`    | (bit: boolean, index: number) => void | -                       | Function to be called with the bit value and index.         |
| `start` | number                                | 0                       | Index of the first bit to look at.                          |
| `end`   | number                                | this.buffer.length \* 8 | Index of the first bit that should no longer be considered. |

**Returns:** void

---

### get

▸ **get**(`i`: number): boolean

Get a particular bit.

#### Parameters:

| Name | Type   | Description            |
| ---- | ------ | ---------------------- |
| `i`  | number | Bit index to retrieve. |

**Returns:** boolean

A boolean indicating whether the `i`th 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:

| Name    | Type    | Default value | Description                                  |
| ------- | ------- | ------------- | -------------------------------------------- |
| `i`     | number  | -             | Bit index to set.                            |
| `value` | boolean | true          | Value 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:

| Name     | Type                 | Default value | Description                           |
| -------- | -------------------- | ------------- | ------------------------------------- |
| `array`  | `ArrayLike<boolean>` | -             | Array of booleans to set the bits to. |
| `offset` | number               | 0             | Index of the first bit to set.        |

**Returns:** void

## License

MIT

---
_Source: https://npm.io/package/bitfield · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
