Kit
Functions

getBitArrayCodec

getBitArrayCodec<TSize>(size, config?): FixedSizeCodec<boolean[], boolean[], TSize>

Returns a codec that encodes and decodes boolean arrays as compact bit representations.

This codec efficiently stores boolean arrays as bits, packing 8 values per byte. The backward config option determines whether bits are stored in MSB-first (false) or LSB-first (true).

Type Parameters

Type ParameterDescription
TSize extends numberThe number of bytes used to store the bit array.

Parameters

ParameterTypeDescription
sizeTSizeThe number of bytes allocated for the bit array (must be sufficient for the expected boolean count).
config?boolean | BitArrayCodecConfigConfiguration options for encoding and decoding the bit array.

Returns

FixedSizeCodec<boolean[], boolean[], TSize>

A FixedSizeCodec<boolean[], boolean[], TSize> for encoding and decoding bit arrays.

Examples

Encoding and decoding a bit array.

const codec = getBitArrayCodec(1);
 
codec.encode([true, false, true, false, false, false, false, false]);
// 0xa0 (0b10100000)
 
codec.decode(new Uint8Array([0xa0]));
// [true, false, true, false, false, false, false, false]

Encoding and decoding a bit array backwards.

const codec = getBitArrayCodec(1, { backward: true });
 
codec.encode([true, false, true, false, false, false, false, false]);
// 0x05 (0b00000101)
 
codec.decode(new Uint8Array([0x05]));
// [true, false, true, false, false, false, false, false]

Remarks

Separate getBitArrayEncoder and getBitArrayDecoder functions are available.

const bytes = getBitArrayEncoder(1).encode([true, false, true, false]);
const value = getBitArrayDecoder(1).decode(bytes);

See

On this page