bit-sequence

中文文档
2.0.1发布于 2026/11/03Apache-2.0TypeScriptES Module零依赖构建来源已验证

Turn an arbitrary sequence of bits from a byte array into an integer

原始使用文档
文档目录7 节
  1. Requirements
  2. JavaScript
  3. Example
  4. API
  5. Go
  6. API
  7. License and Copyright

bit-sequence

NPM

Turn an arbitrary sequence of bits from a byte array into an integer

Requirements

Node.js >= 20

JavaScript

Given an Array-like containing bytes (unsigned 8-bit integers), extract an arbitrary sequence of the underlying bits and convert them into an unsigned integer value.

Useful for cases where a sub-sequence of bits within a longer byte sequence is used to form an index, such as in a hash array mapped trie where an index at each level of the tree structure is formed by incremental chunks of a key's hash.

Example

import bitSequence from 'bit-sequence'
import assert from 'node:assert'
const bytes = new Uint8Array([ 0b00010101, 0b10101000, 0b00000000, 0b00000000 ])
//               extract bits from here ^         to here ^
const int = bitSequence(bytes, 7, 11)
assert.strictEqual(int, 0b11010100000) // or `1696`

API

bitSequence(bytes, start, length)

  • bytes is an Array-like containing bytes. It's assumed that it can be accessed with an array indexing operator and that it will return 8-bit integers (< 255). A Uint8Array or Node.js Buffer fits into this category, as does a standard Array, however the 8-bit assumption is not enforced so an Array full of larger integers will yield undefined results.
  • start is an integer bit index to start extraction (not a byte index).
  • length is the number of bits to extract from the bytes array.

Returns an unsigned integer version of the bit sequence. The most significant bit is not interpreted for a two's compliment representation. You should only get positive numbers >= 0.

As per the example above, the assumption is that we are extracting bytes where the least significant bit is to the right, so we extract in the same order as presented by binary literals.

JavaScript's crazy numbers allows us to extract potentially very large bit sequences, but the usual caveats apply at 32-bits and beyond Number.MAX_SAFE_INTEGER.

Go

github.com/rvagg/bit-sequence/go

API

Exports BitSequence(bytes []byte, bitStart uint32, bitLength uint32) (uint32, error) where:

  • bytes is a simple byte array of arbitrary length
  • bitStart is a bit index to start extraction (not a byte index).
  • bitLength is the number of bits to extract from the bytes array. This value can only be a maximum of 32, higher values will be adjusted down.

Returns an unsigned integer version of the bit sequence. The most significant bit is not interpreted for a two's compliment representation. Returns an error if bitLength exceeds 32 or the requested sequence overflows the bytes boundary.

Copyright 2019 Rod Vagg

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

版本与发布记录

bit-sequence 共发布 22 个版本,首次发布于 2019/07/24;过去 12 个月发布了 20 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.1latest2026/11/03今天
2.0.02026/10/277 天前
1.2.172026/10/277 天前
1.2.162026/10/1915 天前
1.2.152026/08/032 个月前
1.2.142026/08/033 个月前

关于 bit-sequence 的常见问题

bit-sequence 是什么?

将字节数组中的任意位序列转换为整数

如何安装 bit-sequence?

在项目目录运行 npm install bit-sequence,或使用 pnpm add / yarn add / bun add。

bit-sequence 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

bit-sequence 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

bit-sequence 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

bit-sequence 可以商用吗?

bit-sequence 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

bit-sequence 还在维护吗?

最新版本 2.0.1 发布于 2026/11/03(今天);至今共发布 22 个版本,其中过去 12 个月发布了 20 个;首次发布于 2019/07/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。