Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

bitbuf

A fast and lightweight bit-level mutation library for Python.

Notice: bitbuf is currently a beta proof of concept. The public API is still settling, and performance is not fully optimized yet. A future version is expected to optimize the internal implementation with a native programming language backend.

Installation

pip install bitbuf

Quick Start

from bitbuf import bitbuf

buf = bitbuf.from_bytes(b"\x34\x12")

buf[4:12] = 0xAB
buf <<= 3
buf.append_msb(0b1010, 4)

value = int(buf)
payload = bytes(buf)

Bit position 0 is the least significant bit. Byte conversion always uses little-endian order.

Examples

Create Buffers

from bitbuf import bitbuf

empty = bitbuf()
fixed = bitbuf(0b1010_0101, 8)
from_int = bitbuf.from_int(0x1234, size=16)
from_bytes = bitbuf.from_bytes(b"\x34\x12")
zeros = bitbuf.zeros(16)
ones = bitbuf.ones(8)

Read and Write Bits

buf = bitbuf(0b1010_0101, 8)

lowest = buf[0]      # 1
highest = buf[-1]    # 1
buf[1] = 1           # set bit 1
buf.set_bit(7, 0)    # clear bit 7

Read and Write Bit Ranges

Slices use [start:stop], where start is the LSB-first bit position and stop - start is the width.

buf = bitbuf(0x1234, 16)

field = buf[4:12]
field_bytes = buf.get_bits_as_bytes(4, 8)
field_bytearray = buf.get_bits_as_bytearray(4, 8)
field_slice = buf.slice(4, 8) # return a slice as bitbuf
buf[4:12] = 0xAB

same_field = buf.get_bits(4, 8)
buf.set_bits(0, 0b1111, 4)

Set or Clear Ranges

buf = bitbuf(0, 8)

buf.set_ones(2, 4)   # 0b0011_1100
buf.set_zeros(3, 2)  # 0b0010_0100

Shift In Place

All operations mutate the current buffer.

buf = bitbuf(0b0000_1111, 8)

buf <<= 2            # same as buf.lshift(2)
buf >>= 1            # same as buf.rshift(1)

Append and Delete Bits

buf = bitbuf(0b0011, 4)

buf.append_high(0b101, 3)  # 0b101_0011
buf.append_low(0b10, 2)    # 0b1010011_10

buf.delete_low(2)           # discard low bits
high = buf.pop_high(3)      # remove and return high bits

Replace or Clear Contents

buf = bitbuf(0x1234, 16)

buf.assign(0xAB, 8)
buf.toggle()         # flips all 8 bits
buf.clear()          # keeps len(buf) == 8

Convert Back to Python Types

buf = bitbuf.from_bytes(b"\x34\x12")

as_int = int(buf)
as_bytes = bytes(buf)
as_bytearray = buf.bytearray()
as_hex = hex(buf)

assert as_int == buf.int()
assert as_bytes == buf.bytes()
assert as_bytearray == bytearray(as_bytes)
assert as_hex == buf.hex()

API Reference

InputTypes: TypeAlias = int | bytes | bytearray | memoryview | bitbuf

class bitbuf:
    def __init__(self, value: InputTypes = 0, width: int | None = None): ...
    # Create a mutable LSB-first buffer from an int, bytes-like object, or bitbuf.

    @classmethod
    def zeros(cls, width: int) -> bitbuf: ...
    # Build a zero-filled buffer with the requested width.
    @classmethod
    def ones(cls, width: int) -> bitbuf: ...
    # Build a one-filled buffer with the requested width.
    @classmethod
    def from_buffer(cls, buffer: InputTypes, offset: int, size: int) -> bitbuf: ...
    # Build a buffer from a bit range in a bytes-like source.

    def __eq__(self, other): ...
    # Check if bitbuf object is the same as another.
    def __len__(self) -> int: ...
    # Return width in bits.
    def __int__(self) -> int: ...
    # Convert the buffer to an integer.
    def __index__(self) -> int: ...
    # Allow numeric helpers such as hex(buf).
    def __bytes__(self) -> bytes: ...
    # Convert the buffer to little-endian bytes.
    def __repr__(self) -> str: ...
    # Return a concise debug representation with width and hex data.
    def __getitem__(self, key: int | slice) -> int: ...
    # Read one bit or an integer-valued bit range.
    def __setitem__(self, key: int | slice, value: InputTypes) -> None: ...
    # Write one bit or replace a bit range.
    def __iadd__(self, value: bytes | bytearray | bitbuf) -> bitbuf: ...
    # In-place append sized value to the high side.
    def __ilshift__(self, bits: int) -> Self: ...
    # Shift left in place.
    def __irshift__(self, bits: int) -> Self: ...
    # Shift right in place.

    def assign(self, value: InputTypes = 0, width: int | None = None) -> Self: ...
    # Replace the entire buffer contents and width.
    def resize(self, width: int) -> Self: ...
    # Change width, trimming discarded high bits when shrinking.
    def clear(self) -> Self: ...
    # Zero all bits while keeping the current width.
    def clone(self) -> Self: ...
    # Create a new bitbuf with copied data.

    def get_bit(self, pos: int) -> int: ...
    # Return one bit at the given LSB-first position.
    def get_bits(self, pos: int, width: int) -> int: ...
    # Return a bit range as an integer.
    def get_bits_as_bytes(self, pos: int, width: int) -> bytes: ...
    # Return a bit range as little-endian bytes.
    def get_bits_as_bytearray(self, pos: int, width: int) -> bytearray: ...
    # Return a bit range as little-endian bytearray.
    def slice(self, pos: int, width: int) -> bitbuf: ...
    # Return a bit range as a new bitbuf.

    def clear_bit(self, pos: int) -> Self: ...
    # Clear one bit.
    def set_bit(self, pos: int, value: int = 1) -> Self: ...
    # Set or clear one bit.
    def set_bits(self, pos: int, value: InputTypes = 0, width: int | None = None) -> Self: ...
    # Replace a bit range with a sized value.
    def set_ones(self, pos: int, width: int) -> Self: ...
    # Fill a bit range with ones.
    def set_zeros(self, pos: int, width: int) -> Self: ...
    # Fill a bit range with zeros.
    def toggle(self, pos: int = 0, width: int | None = None) -> Self: ...
    # Flip all bits from pos upward, or a specific range when width is given.

    def lshift(self, bits: int) -> Self: ...
    # Shift bits toward the high side while preserving width.
    def rshift(self, bits: int) -> Self: ...
    # Shift bits toward the low side while preserving width.

    def append_low(self, value: InputTypes = 0, width: int | None = None) -> Self: ...
    # Grow the buffer by appending bits on the low side.
    def append_high(self, value: InputTypes = 0, width: int | None = None) -> Self: ...
    # Grow the buffer by appending bits on the high side.
    def delete_low(self, width: int) -> Self: ...
    # Discard low-side bits without returning them.
    def delete_high(self, width: int) -> Self: ...
    # Discard high-side bits without returning them.
    def pop_low(self, width: int) -> int: ...
    # Remove and return low-side bits.
    def pop_high(self, width: int) -> int: ...
    # Remove and return high-side bits.

    def bytearray(self) -> bytearray: ...
    # Return the whole buffer as little-endian bytearray.
    def bytes(self) -> bytes: ...
    # Return the whole buffer as little-endian bytes.
    def hex(self) -> str: ...
    # Return hex(self.int()).
    def int(self) -> int: ...
    # Return the whole buffer as an integer.

    @property
    def width(self) -> int: ...
    # Width of the buffer in bits.
    @property
    def nbytes(self) -> int: ...
    # Minimum byte count needed to store the buffer width.

Development

python -m pip install -e ".[test]"
python -m pytest

Metadata

Release files for bitbuf 0.4.0b0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bitbuf 0.4.0b0
File Size Uploaded Provenance Actions
bitbuf-0.4.0b0.tar.gz 34.2 kB With provenance Details for bitbuf-0.4.0b0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for bitbuf 0.4.0b0
File Actions
bitbuf-0.4.0b0-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details for bitbuf-0.4.0b0-cp314-cp314-win_amd64.whl
bitbuf-0.4.0b0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 Details for bitbuf-0.4.0b0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
bitbuf-0.4.0b0-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details for bitbuf-0.4.0b0-cp313-cp313-win_amd64.whl
bitbuf-0.4.0b0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 Details for bitbuf-0.4.0b0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
bitbuf-0.4.0b0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for bitbuf-0.4.0b0-cp312-cp312-win_amd64.whl
bitbuf-0.4.0b0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for bitbuf-0.4.0b0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
bitbuf-0.4.0b0-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details for bitbuf-0.4.0b0-cp311-cp311-win_amd64.whl
bitbuf-0.4.0b0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for bitbuf-0.4.0b0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
bitbuf-0.4.0b0-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details for bitbuf-0.4.0b0-cp310-cp310-win_amd64.whl
bitbuf-0.4.0b0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for bitbuf-0.4.0b0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Total release size: 1.8 MB

Release files / bitbuf-0.4.0b0.tar.gz

Download URL bitbuf-0.4.0b0.tar.gz
Size 34.2 kB
Tags Source
SHA-256 checksum
How to use checksums
22e4a2d2f9506c6946f3a952ad98ca169e124dd165556195f3232066acb1286d
BLAKE2b-256 checksum
How to use checksums
65e2045e8da16f6b74c7a976ce6fc0ec5b7700c65124d160a1b1e51251fea8f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / bitbuf-0.4.0b0-cp314-cp314-win_amd64.whl

Download URL bitbuf-0.4.0b0-cp314-cp314-win_amd64.whl
Size 273.7 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
8a959a6e3a5b89808b2107fa97c0737c15de12a5cd412d54fa80033cf8beb506
BLAKE2b-256 checksum
How to use checksums
92e32d8afb0d3dd6cd8021374f790c934e9c1db0b2b63aad9fc56faabaea0055
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / bitbuf-0.4.0b0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL bitbuf-0.4.0b0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 92.5 kB
Tags CPython 3.14 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
be73f9a2680856e0e14ed110b8239660cfd4e7dd523f42a4b57127f624632fac
BLAKE2b-256 checksum
How to use checksums
665cd39a68527702d1ccd27b4b0bf45f5ecb61a51cff62dc01f23de6d7d92a82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / bitbuf-0.4.0b0-cp313-cp313-win_amd64.whl

Download URL bitbuf-0.4.0b0-cp313-cp313-win_amd64.whl
Size 265.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
3105c71501e9c434b7afa6d1b957f103515f68ee1dc66df4380ce9419341e5bb
BLAKE2b-256 checksum
How to use checksums
fe122c59c83d748c5eb8bfb4e51b9dde5d905ba653108ad88f49d05bfff92c4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / bitbuf-0.4.0b0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL bitbuf-0.4.0b0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 92.4 kB
Tags CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
647ba6fc35c2b142d6f3d73dc4c21c9132d096a9102b6fbf43dad1e8ca74ad88
BLAKE2b-256 checksum
How to use checksums
ad21b18d03eae5c7418d19e7d82aaae8e3575e40d195cfda2bd061d83d374398
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / bitbuf-0.4.0b0-cp312-cp312-win_amd64.whl

Download URL bitbuf-0.4.0b0-cp312-cp312-win_amd64.whl
Size 266.0 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
4594344139472015c43b6d3f4fcbbb4259d356d8c716ece02a1db6a334f5fdbd
BLAKE2b-256 checksum
How to use checksums
c8ea44ff3a1d6484ee6d08b9f037dd2fa0a25a1afc3fda961daa1accb0a5af0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / bitbuf-0.4.0b0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL bitbuf-0.4.0b0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 92.7 kB
Tags CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
daa2fa049e96ab05915306fabeae48f38293e24083d18cdb75d40bd3c5bef138
BLAKE2b-256 checksum
How to use checksums
47fffc62637d2501721dd9b42f6c723cda75be1351cdab6f17d6967b925e7c99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / bitbuf-0.4.0b0-cp311-cp311-win_amd64.whl

Download URL bitbuf-0.4.0b0-cp311-cp311-win_amd64.whl
Size 267.4 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
8e67ad17061193fffcb29a14081e06f3ccb959f4ed9b713ccbc08915951248b8
BLAKE2b-256 checksum
How to use checksums
e95ba672b9b8361c52e94e7a11e7ab88f7cc775efb7f398e4339b44f5fbeb763
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / bitbuf-0.4.0b0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL bitbuf-0.4.0b0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 95.5 kB
Tags CPython 3.11 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
91a05dfd7b985e6bdb5a005c9dd45e0a54df27b00b2dc379e62fdf5d8654bc00
BLAKE2b-256 checksum
How to use checksums
7f8e4e5a9bc2b264d69efff98fd098936d485893810afcfed7ed6a9e5bcbacfe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / bitbuf-0.4.0b0-cp310-cp310-win_amd64.whl

Download URL bitbuf-0.4.0b0-cp310-cp310-win_amd64.whl
Size 267.3 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
e96c171fc13818fc81a448918154ca205d9eab487d5d71d766114955cd4ed776
BLAKE2b-256 checksum
How to use checksums
3949d2ad0e660f45c135665e19c55da405c2117bd79d733bb1852bb531549782
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / bitbuf-0.4.0b0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL bitbuf-0.4.0b0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 96.1 kB
Tags CPython 3.10 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
9ecaffff07ea0956e5751969c0b6fbd78b0ef210c4dacf36c26dbdf88e2ed278
BLAKE2b-256 checksum
How to use checksums
006c3daa8d02c7dabe39e1061162350d30c7026d0d8bb2cc1afd4ead0c6efe38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Security for bitbuf 0.4.0b0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 11 files in this release have been uploaded with provenance metadata.

Select from 11 files

Attestation for bitbuf-0.4.0b0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page