--- id: bitstruct version: "8.22.1" license: MIT license_treatment: permissive maintenance: active --- # bitstruct — This module performs conversions between Python values and C bit field structs represented as Python byte strings. License: permissive · Maintenance: active · Downloads: 5.1M/mo ## What it is and what it does Bitstruct converts between Python values and bit-level binary structures, extending the interface of Python's built-in struct module to work on individual bits instead of bytes. It provides functions to pack values into compact binary representations and unpack them back, with support for unsigned integers, signed integers, floats, booleans, raw bytes, and text strings at arbitrary bit boundaries. Format strings specify bit widths and types (e.g., 'u1u3u4s16' for a 1-bit unsigned, 3-bit unsigned, 4-bit unsigned, and 16-bit signed integer), and compiled format objects allow reuse of the same specification across multiple operations. It supports bit-offset packing into existing buffers, dictionary-based packing/unpacking, and byte-order control. The library includes both a pure Python implementation and optional C accelerations for faster pack/unpack operations on CPython 3. Use it for: - Parse binary network protocols or file formats with non-byte-aligned fields (e.g., bit flags, variable-width integers) - Serialize structured data into compact binary representations for embedded systems or bandwidth-constrained communication - Extract and manipulate individual bit fields from hardware register dumps or low-level binary data - Implement bit-level codecs for compression or encoding schemes that operate below byte boundaries - Test and validate binary protocol implementations by packing/unpacking known test vectors ## Worth the install? AI-flagged interpretation of the facts above — verify before relying. Packs and unpacks Python values into bit-level binary structures, similar to the struct module but operating on individual bits rather than bytes. Yes, if you need to work with bit-level binary data. Bitstruct is stable (Production/Stable status), actively maintained, has no dependencies, and provides a clear interface for a specialized but real problem. The optional C acceleration and wide platform support make it practical for both development and performance-critical code. No security vulnerabilities reported. ## Install pip install bitstruct uv add bitstruct poetry add bitstruct ## Installing bitstruct Before you install: Medium install friction due to compiled C extensions for multiple platforms (wheels provided for CPython 3.10–3.11 across Linux, macOS, Windows, and ARM architectures). Active maintenance with recent releases; no runtime dependencies. License in practice: MIT license permits unrestricted use, modification, and distribution in both open and closed-source projects with minimal attribution requirements. Quickstart: pip install bitstruct from bitstruct import pack, unpack packed = pack('u1u3u4s16', 1, 2, 3, -4) print(packed) # b'\xa3\xff\xfc' unpacked = unpack('u1u3u4s16', packed) print(unpacked) # (1, 2, 3, -4) Requires Python 3.9 or later. C implementation available only on CPython 3 and must be explicitly imported; pure Python fallback is default. Verify before relying: - Whether the C implementation is automatically selected or requires explicit import on supported platforms - Performance characteristics and speedup magnitude of C vs. pure Python implementation for typical workloads - Compatibility with Python 3.12+ beyond the stated 3.9+ requirement ## Package facts - License: MIT (permissive) - Python support: supports_current - Install friction: medium - Maintenance: active - Downloads: 5.1M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags bit packing unpacking, binary bit field parsing, struct module bits, bit-level serialization, binary protocol parsing, bit manipulation, compact binary encoding, binary-serialization, bit-manipulation, protocol-parsing [View on SkillFed](https://skillfed.io/packages/bitstruct) · [View on PyPI](https://pypi.org/project/bitstruct/)