Skip to main content

Mutable LSB-first bit buffer utilities.

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(4, 0b1010)

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(8, 0b1010_0101)
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(8, 0b1010_0101)

lowest = buf[0]      # 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(16, 0x1234)

field = buf[4:12]
buf[4:12] = 0xAB

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

Set or Clear Ranges

buf = bitbuf(8, 0)

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(8, 0b0000_1111)

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

Append and Delete Bits

buf = bitbuf(4, 0b0011)

buf.append_msb(3, 0b101)  # 0b101_0011
buf.append_lsb(2, 0b10)   # 0b1010011_10

low = buf.delete_lsb(2)
high = buf.delete_msb(3)

Convert Back to Python Types

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

as_int = int(buf)
as_bytes = bytes(buf)

assert as_int == buf.toint()
assert as_bytes == buf.tobytes()

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.1.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

bitbuf-0.1.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file bitbuf-0.1.1.tar.gz.

File metadata

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

File hashes

Hashes for bitbuf-0.1.1.tar.gz
Algorithm Hash digest
SHA256 91befbbf6277f4660c1ee1c8c90ee3a7e8a237faef5c705bf5e74364be4d4e05
MD5 7389a4ffa7649c38cc3e56161be69542
BLAKE2b-256 832bd5e0c5d8321ab3cb118505c7c5a5abd12a093f35f6592067ddc3942e24bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.1.1.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.1.1-py3-none-any.whl.

File metadata

  • Download URL: bitbuf-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for bitbuf-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a0e892f8a135d71aa023d0cf89bcd4a4ef6375b4a71c29da39b0cba23a206f73
MD5 1a716b787dd878f9ac6cdaac31ec5752
BLAKE2b-256 a04e439e1fcb2320d1f4e7ba521ad6e288bee714fff065776ec5804f80da575e

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitbuf-0.1.1-py3-none-any.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