Skip to main content

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

Project description

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).toggle() # assign and then 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 from_int(cls, data: int, width: int | None = None) -> bitbuf: ...
    # Build a buffer from an integer, inferring width from bit_length when omitted.
    @classmethod
    def from_bytes(cls, data: bytes | bytearray | memoryview, width: int | None = None) -> bitbuf: ...
    # Build a buffer from little-endian bytes-like data.
    @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.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bitbuf-0.3.0b0.tar.gz (28.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

bitbuf-0.3.0b0-cp314-cp314-win_amd64.whl (25.1 kB view details)

Uploaded CPython 3.14Windows x86-64

bitbuf-0.3.0b0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (129.0 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

bitbuf-0.3.0b0-cp313-cp313-win_amd64.whl (24.8 kB view details)

Uploaded CPython 3.13Windows x86-64

bitbuf-0.3.0b0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (103.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

bitbuf-0.3.0b0-cp312-cp312-win_amd64.whl (24.8 kB view details)

Uploaded CPython 3.12Windows x86-64

bitbuf-0.3.0b0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (78.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

bitbuf-0.3.0b0-cp311-cp311-win_amd64.whl (24.7 kB view details)

Uploaded CPython 3.11Windows x86-64

bitbuf-0.3.0b0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (53.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

bitbuf-0.3.0b0-cp310-cp310-win_amd64.whl (24.7 kB view details)

Uploaded CPython 3.10Windows x86-64

bitbuf-0.3.0b0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (27.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file bitbuf-0.3.0b0.tar.gz.

File metadata

  • Download URL: bitbuf-0.3.0b0.tar.gz
  • Upload date:
  • Size: 28.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for bitbuf-0.3.0b0.tar.gz
Algorithm Hash digest
SHA256 a03ea379ccea2f014ef8da693a766fee680403cf22c233744fb13b8e00594f58
MD5 6016d6062c432c2b6ce1648a92a930a9
BLAKE2b-256 b297fd48746f4f73ffbeeb8b8a2ae3ab0aeb9dbf380e2e2bcfcfe2344dae534c

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.3.0b0.tar.gz:

Publisher: release.yml on donlon/bitbuf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitbuf-0.3.0b0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: bitbuf-0.3.0b0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 25.1 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for bitbuf-0.3.0b0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 cfb8ec122eafa1d517a4a552a84b4e2f6eda97237fab1383bf3525359859bf40
MD5 c6408ca62eab042787d2cae8a62f061c
BLAKE2b-256 340baa66f9e0bedbe0a38b376ece6a2251125f556cde9aff237fb8975fa150fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.3.0b0-cp314-cp314-win_amd64.whl:

Publisher: release.yml on donlon/bitbuf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitbuf-0.3.0b0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for bitbuf-0.3.0b0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d5fb670f32bc3af3e5d94281e0eb0efb6a35f2bfbcc1c1567ff69b91b65f4a18
MD5 5db4f2241b32e07374e5d51874f0cd53
BLAKE2b-256 939551e2682ed6c621a28ad295bccec9e59fe629f3f085a14ed2f7cbbcf95ca9

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.3.0b0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on donlon/bitbuf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitbuf-0.3.0b0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: bitbuf-0.3.0b0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for bitbuf-0.3.0b0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 bfd307fa95ca5dcc3a324eaef1615b30cd7ee9dcefb7060858defb27dfa33b80
MD5 0db63ece4718d36092011b4d0851e806
BLAKE2b-256 806447bcae07145ab93adf7de88a49e158230828d236d5c73725513a25d1ac17

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.3.0b0-cp313-cp313-win_amd64.whl:

Publisher: release.yml on donlon/bitbuf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitbuf-0.3.0b0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for bitbuf-0.3.0b0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 31f7baf203d0653311317c0ac99c1991216a5749a03d500af567d06088efe337
MD5 666e3cf8c171bab0c2da37bcedc86b71
BLAKE2b-256 dfe3c3d17798b57ea101dd35b0aa4af21630bfebe87e81e53885423d56ed5e9b

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.3.0b0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on donlon/bitbuf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitbuf-0.3.0b0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: bitbuf-0.3.0b0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for bitbuf-0.3.0b0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a31ff9fadf21388a4b1466d1fcaa0b11263eb343ef1c43d798938c9c714c9c05
MD5 a79ee21e6e14ef3d02db97e0ed5ab64e
BLAKE2b-256 e4dd3573d0772e5e60d306bc5de7f7c1b9a131a2424782710c42a3df8f712ba0

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.3.0b0-cp312-cp312-win_amd64.whl:

Publisher: release.yml on donlon/bitbuf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitbuf-0.3.0b0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for bitbuf-0.3.0b0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e75ec62c3d396b6ece34be26091119e912454f16c8cbabd2aabb1f45147cf2fc
MD5 4a410ca10b4d1aa81ecd5b72f9113a89
BLAKE2b-256 3a9f3d738499dc9bebf78f664d6011b76f455be26faf5462acc4aa8dadac5b83

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.3.0b0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on donlon/bitbuf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitbuf-0.3.0b0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: bitbuf-0.3.0b0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for bitbuf-0.3.0b0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0560dbea0ee186a4ca70259831a4cdcda58c7541ae72146d04586b177eaaa42d
MD5 0938b0ec3c979db4c569ee5b8d7dff0a
BLAKE2b-256 ad1c76f0c7418d8fbd3a767c7693247db0a31bdce5638de990a700353eb67af2

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.3.0b0-cp311-cp311-win_amd64.whl:

Publisher: release.yml on donlon/bitbuf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitbuf-0.3.0b0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for bitbuf-0.3.0b0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9117fda8b9514c26f013ee87c44089762f17657bbf776cc359662a26ed7d948b
MD5 c0cb6aa3689b122f6792a43c6ab7e437
BLAKE2b-256 a05c2d103de105c5c40dc975448e1ace65f2e21ccaa787fd276c53fabdfb6aa4

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.3.0b0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on donlon/bitbuf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitbuf-0.3.0b0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: bitbuf-0.3.0b0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for bitbuf-0.3.0b0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 988cc32bbc08aceb275a135778ee1d8b95367bdcedc33f7b4f40643982e4e8ea
MD5 6582425154d221f9868171fac6f6bba7
BLAKE2b-256 d17ffb2dc8cb28ac84fe25c32c00e1e60ac607fc9d9d3f0897081feaf6d0bb77

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.3.0b0-cp310-cp310-win_amd64.whl:

Publisher: release.yml on donlon/bitbuf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitbuf-0.3.0b0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for bitbuf-0.3.0b0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ebca33e5f699b7b26b2f3cf9ac4f0cf7b50a41669172dd174b5edb8679e99d0e
MD5 24747e4f5058da0c7234a996c04aeae8
BLAKE2b-256 e00ba705762c9c0acc551e5e0d69226046f1a2679b36503c55c72b1798590105

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.3.0b0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on donlon/bitbuf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page