# bit-buffer

> Bit-level reads and writes for ArrayBuffers

Latest version **0.3.0** (published 2025-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install bit-buffer
pnpm add bit-buffer
yarn add bit-buffer
bun add bit-buffer
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2025-11-04 |
| First published | 2013-03-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 32.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 51 |
| Author | Anthony Pesch |
| Maintainers | inolen, icewind1991 |
| Keywords | dataview, arraybuffer, bit, bits |

## Links

- npm: https://www.npmjs.com/package/bit-buffer
- Repository: https://github.com/inolen/bit-buffer
- Homepage: https://github.com/inolen/bit-buffer#readme
- Issues: https://github.com/inolen/bit-buffer/issues
- npm.io page: https://npm.io/package/bit-buffer

## 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

- 0.3.0 (latest) — 2025-11-04
- 0.2.5 — 2021-01-30
- 0.2.4 — 2018-12-05
- 0.2.3 — 2017-09-05
- 0.2.2 — 2017-09-05
- 0.2.1 — 2017-09-05
- 0.2.0 — 2017-08-12
- 0.1.0 — 2017-03-12
- 0.0.3 — 2013-08-12
- 0.0.2 — 2013-08-12
- 0.0.1 — 2013-03-24

## README

# BitBuffer

![Node.js CI](https://github.com/inolen/bit-buffer/workflows/Node.js%20CI/badge.svg)

BitBuffer provides two objects, `BitView` and `BitStream`. `BitView` is a wrapper for ArrayBuffers, similar to JavaScript's [DataView](https://developer.mozilla.org/en-US/docs/JavaScript/Typed_arrays/DataView), but with support for bit-level reads and writes. `BitStream` is a wrapper for a `BitView` used to help maintain your current buffer position, as well as to provide higher-level read / write operations such as for ASCII strings.

## BitView

### Attributes

```javascript
bb.buffer  // Underlying ArrayBuffer.
```

```javascript
bb.bigEndian = true; // Switch to big endian (default is little)
```

### Methods

#### BitView(buffer, optional byteOffset, optional byteLength)

Default constructor, takes in a single argument of an ArrayBuffer. Optional are the `byteOffset` and `byteLength` arguments to offset and truncate the view's representation of the buffer.

### getBits(offset, bits, signed)

Reads `bits` number of bits starting at `offset`, twiddling the bits appropriately to return a proper 32-bit signed or unsigned value. NOTE: While JavaScript numbers are 64-bit floating-point values, we don't bother with anything other than the first 32 bits.

### getInt8, getUint8, getInt16, getUint16, getInt32, getUint32(offset)

Shortcuts for getBits, setting the correct `bits` / `signed` values.

### getFloat32(offset)

Gets 32 bits from `offset`, and coerces and returns as a proper float32 value.

### getFloat64(offset)

Gets 64 bits from `offset`, and coerces and returns as a proper float64 value.

### setBits(offset, value, bits)

Sets `bits` number of bits at `offset`.

### setInt8, setUint8, setInt16, setUint16, setInt32, setUint32(offset)

Shortcuts for setBits, setting the correct `bits` count.

### setFloat32(offset)

Coerces a float32 to uint32 and sets at `offset`.

### setFloat64(offset)

Coerces a float64 to two uint32s and sets at `offset`.


## BitStream

### Attributes

```javascript
bb.view;  // Underlying BitView
```

```javascript
bb.length; // Get the length of the stream in bits
```

```javascript
bb.bitsLeft; // The number of bits left in the stream
```

```javascript
bb.index; // Get the current index in bits
bb.index = 0// Set the current index in bits
```

```javascript
bb.bigEndian = true; // Switch to big endian (default is little)
```

### Methods

#### BitStream(view)

Default constructor, takes in a single argument of a `BitView`, `ArrayBuffer` or node `Buffer`.

#### BitSteam(buffer, optional byteOffset, optional byteLength)

Shortcut constructor that initializes a new `BitView(buffer, byteOffset, byteLength)` for the stream to use.

#### readBits(bits, signed)

Returns `bits` numbers of bits from the view at the current index, updating the index.

#### writeBits(value, bits)

Sets `bits` numbers of bits from `value` in the view at the current index, updating the index.

#### readUint8(), readUint16(), readUint32(), readInt8(), readInt16(), readInt32()
 
Read a 8, 16 or 32 bits (unsigned) integer at the current index, updating the index.

#### writeUint8(value), writeUint16(value), writeUint32(value), writeInt8(value), writeInt16(value), writeInt32(value)
 
Write 8, 16 or 32 bits from `value` as (unsigned) integer at the current index, updating the index.

#### readFloat32(), readFloat64()

Read a 32 or 64 bit floating point number at the current index, updating the index.

#### writeFloat32(value), writeFloat64()

Set 32 or 64 bits from `value` as floating point value at the current index, updating the index.

#### readBoolean()

Read a single bit from the view at the current index, updating the index.

#### writeBoolean(value)

Write a single bit to the view at the current index, updating the index.

#### readASCIIString(optional bytes), readUTF8String(optional bytes)

Reads bytes from the underlying view at the current index until either `bytes` count is reached or a 0x00 terminator is reached. 

#### writeASCIIString(string, optional bytes), writeUTF8String(string, optional bytes)

Writes a string followed by a NULL character to the underlying view starting at the current index. If the string is longer than `bytes` it will be truncated, and if it is shorter 0x00 will be written in its place.

#### readBytes(size)

Read a `size` bytes into a new Uint8Array instance.

#### writeBytes(buffer, size)

Write `size` bytes from a given Uint8Array instance.

## license

MIT

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