# compact-encoding-bitfield

> Compact codec for bitfields

Latest version **1.1.0** (published 2026-05-05) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install compact-encoding-bitfield
pnpm add compact-encoding-bitfield
yarn add compact-encoding-bitfield
bun add compact-encoding-bitfield
```

## Health

**Score 45/100 (D)** — status: active.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2026-05-05 |
| First published | 2022-05-02 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 15.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Holepunch |
| Maintainers | mafintosh |

## Links

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

## Dependencies (1)

- [compact-encoding](https://npm.io/package/compact-encoding.md) ^3.0.0

## Recent versions

- 1.1.0 (latest) — 2026-05-05
- 1.0.0 — 2022-05-02

## README

# compact-encoding-bitfield

[compact-encoding](https://github.com/compact-encoding/compact-encoding) codec for bitfields. Uses a variable length encoding that is ABI compatible with `cenc.uint`.

```sh
npm install compact-encoding-bitfield
```

## Usage

```js
const cenc = require('compact-encoding')
const bitfield = require('compact-encoding-bitfield')

const buffer = cenc.encode(bitfield(8), 0b1110_1011)
// <Buffer fd eb 00>

cenc.decode(bitfield(8), buffer)
// <Buffer eb>
```

Bitfields are represented as buffers. For each byte, the least significant bit denotes the first bit and the most significant bit denotes the last bit. For example, bit `0` will be the least significant bit of the first byte and bit `15` will be the most significant bit of the second byte.

The encoding will convert numbers to buffers. Buffers may also be passed directly and will be returned on decode.

## ABI

Depending on `length`, the bitfield is stored using a variable number of bytes:

1. `length < 8`: 1 byte with no prefix.
2. `length <= 16`: 2 bytes with a `0xfd` prefix.
3. `length <= 32`: 4 bytes with a `0xfe` prefix.
4. `length <= 64`: 8 bytes with a `0xff` prefix.

`length` must not exceed 64 bits.

## License

Apache 2.0

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