npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

bit-sequence

v1.1.0

Published

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

Downloads

263

Readme

bit-sequence

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

JavaScript

NPM

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

const bitSequence = require('bit-sequence')
const assert = require('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.

License and Copyright

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.