Skip to main content

bitformat

A Python library for creating and parsing binary formats.

PyPI - Version CI badge Docs Codacy Badge


bitformat is a Python module for creating, manipulating and interpreting binary data. It also supports parsing and creating more complex binary formats.

It is from the author of the widely used bitstring module.


TLDR;

Here are a few code snippets to whet the appetite. If anything here looks useful to you then bitformat might be what you need.

Creating and manipulating bits:

    >>> from bitformat import MutableBits, Format, Array
    >>> b = MutableBits('0x123')
    >>> b += '0b110'
    >>> b.replace('0b11', '0xf')
    MutableBits('0b0001001000111111110')
    >>> b.unpack('(u15, [bool;])')
    (2335, (True, True, True, False))
    >>> list(b.to_bits().find_all('0b10'))
    [3, 6, 17]

Arrays of bits of a single data type:

    >>> a = Array.from_bits('i5', '0xfeed5')
    >>> a
    Array('i5', [-1, -5, -10, -11])
    >>> a + 10
    Array('i5', [9, 5, 0, -1])
    >>> a.dtype = 'bin4'
    >>> a
    Array('bin4', ['1111', '1110', '1110', '1101', '0101'])

Format creation and parsing:

    >>> f = Format("fmt: (x: u3, arr: [i4; {x}], tuple(f16, f32), repeat {x + 1}: bool)")
    >>> f.parse(b'some_byte_data')
    67
    >>> f.pp()
    fmt: (
        x: u3 = 3,
        arr: [i4; {x}] = (-7, -5, 7),
        tuple(f16, f32) = (-0.41845703125, -3.2239261260613716e-10),
        repeat{x + 1}:
            bool = False
            bool = False
            bool = True
            bool = True
    )
    >>> f['arr'].value = (1, 2, 3)
    >>> f.to_bytes()
    b'bGme_byt`'

Features

  • The Bits and MutableBits classes represent sequences of binary data of arbitrary length. They provide methods for creating, modifying and interpreting the data.
  • The Format class provides a way to define a binary format using a simple and flexible syntax.
  • A wide array of data types is supported, with no arbitrary restrictions on length.
  • Data is always stored as a contiguous array of bits.
  • The core is written in Rust for maximum performance.

[!NOTE] To see what has been added, improved or fixed, and also to see what's coming in the next version, see the release notes.

Installation

There should be pre-built wheels available for most platforms so you can just pip install bitformat. If a wheel isn't available then please file a bug report and optionally take a look at the build.sh script.

Documentation

Some Examples

Creating some Bits

A variety of constructor methods are available to create Bits, including from binary, hexadecimal or octal strings, formatted strings, byte literals and iterables.

>>> from bitformat import *

>>> a = Bits('0b1010')  # Create from a binary string
>>> b = Bits('u12 = 54')  # Create from a formatted string.
>>> c = Bits.from_bytes(b'\x01\x02\x03')  # Create from a bytes or bytearray object.
>>> d = Bits.from_dtype('f16', -0.75)  # Pack a value into a data type.
>>> e = Bits.from_joined([a, b, c, d])  # The best way to join lots of bits together.

Interpreting those Bits

Although the examples above were created from a variety of data types, the Bits instance doesn't retain any knowledge of how it was created - it's just a sequence of bits. You can therefore interpret them however you'd like:

>>> a.i
-6
>>> b.hex
'036'
>>> c.unpack(['u4', 'f16', 'u4'])
[0, 0.0005035400390625, 3]
>>> d.bytes
b'\xba\x00'

The unpack method is available as a general-case way to unpack the bits into a single or multiple data types. If you only want to unpack to a single data type you can use properties of the Bits as a short-cut.

Data types

A wide range of data types are supported. These are essentially descriptions on how binary data can be converted to a useful value. The Dtype class is used to define these, but usually just the string representation can be used.

Some example data type strings are:

  • 'u3' - a 3 bit unsigned integer.
  • 'i32_le' - a 32 bit little-endian signed integer.
  • 'f64' - a 64 bit IEEE float. Lengths of 16, 32 and 64 are supported.
  • 'bool' - a single bit boolean value.
  • 'bytes10' - a 10 byte sequence.
  • 'hex' - a hexadecimal string.
  • 'bin' - a binary string.
  • '[u8; 40]' - an array of 40 unsigned 8 bit integers.

Byte endianness for floating point and integer data types is specified with _le, _be and _ne suffixes to the base type.

Bit operations

An extensive set of operations are available to query Bits or to create new ones. For example:

>>> a + b  # Concatenation
Bits('0xa036')
>>> c.find('0b11')  # Returns found bit position
22
>>> b.replace('0b1', '0xfe')
Bits('0x03fbf9fdfc')
>>> b[0:10] | d[2:12]  # Slicing and logical operators
Bits('0b1110101101')

Arrays

An Array class is provided which stores a contiguous sequence of Bits of the same data type. This is similar to the array type in the standard module of the same name, but it's not restricted to just a dozen or so types.

>>> r = Array('i5', [4, -3, 0, 1, -5, 15])  # An array of 5 bit signed ints
>>> r -= 2  # Operates on each element
>>> r.to_list()
[2, -5, -2, -1, -7, 13]
>>> r.dtype = 'u6'  # You can freely change the data type
>>> r
Array('u6', [5, 47, 55, 60, 45])
>>> r.to_bits()
Bits('0b000101101111110111111100101101')

A Format example

The Format class can be used to give structure to bits, as well as storing the data in a human-readable form.

>>> f = Format('(width: u12, height: u12, flags: [bool; 4])')
>>> f.pack([320, 240, [True, False, True, False]])
>>> print(f)
(
    width: u12 = 320
    height: u12 = 240
    flags: [bool; 4] = (True, False, True, False)
)
>>> f['height'].value /= 2
>>> f.to_bits()
Bits('0x140078a')
>>> f.to_bits() == 'u12=320, u12=120, 0b1010'
True

The Format and its fields can optionally have names (the Format above is unnamed, but its fields are named). In this example the pack method was used with appropriate values, which then returned a Bits object. The Format now contains all the interpreted values, which can be easily accessed and modified.

The final line in the example above demonstrates how new Bits objects can be created when needed by promoting other types, in this case the formatted string is promoted to a Bits object before the comparison is made.

The Format can be used symmetrically to both create and parse binary data:

>>> f.parse(b'x\x048\x10')
28
>>> f
Format([
    'width: u12 = 1920',
    'height: u12 = 1080',
    'flags: [bool; 4] = (False, False, False, True)'
])

The parse method is able to lazily parse the input bytes, and simply returns the number of bits that were consumed. The actual values of the individual fields aren't calculated until they are needed, which allows large and complex file formats to be efficiently dealt with.

More to come

The bitformat library is still in beta and is being actively developed. The first release was in September 2024, with the second arriving in January 2025 and the third in April. Version 0.5 was released in June 2025. More features are being added steadily.

There are a number of important features planned, some of which are from the bitstring library on which much of the core is based, and others are needed for a full binary format experience.

The (unordered) :todo: list includes:

  • Streaming methods. :sparkles: Done in v0.2 - see the Reader class :sparkles: .
  • Field expressions. :sparkles: Done in v0.3 :sparkles: Rather than hard-coding everything in a field, some parts will be calculated during the parsing process. For example in the format '(w: u16, h: u16, [u8; {w * h}])' the size of the 'u8' array would depend on the values parsed just before it.
  • New field types. :sparkles: Done in v0.3 :sparkles: Fields like Repeat and If are planned which will allow more flexible formats to be written.
  • Exotic floating point types. In bitstring there are a number of extra floating point types such as bfloat and the MXFP 8, 6 and 4-bit variants. These will be ported over to bitformat.
  • Performance improvements. A primary focus on the design of bitformat is that it should be fast. Version 0.5 received some significant speed boosts and is now competitive for most use cases.
  • LSB0. Currently all bit positions are done with the most significant bit being bit zero (MSB0). I plan to add support for least significant bit zero (LSB0) bit numbering as well.

Copyright (c) 2024-2025 Scott Griffiths

Metadata

Release files for bitformat 0.9.0

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

Source distribution (sdist)

Source distribution for bitformat 0.9.0
File Size Uploaded
bitformat-0.9.0.tar.gz 3.7 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for bitformat 0.9.0
File
bitformat-0.9.0-cp314-cp314t-win_amd64.whl CPython 3.14 CPython 3.14 free-threading Windows x86-64 Details
bitformat-0.9.0-cp314-cp314t-win32.whl CPython 3.14 CPython 3.14 free-threading Windows x86-32 Details
bitformat-0.9.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details
bitformat-0.9.0-cp311-abi3-win32.whl CPython 3.11 abi3 Windows x86-32 Details
bitformat-0.9.0-cp311-abi3-musllinux_1_2_x86_64.whl CPython 3.11 abi3 Linux musl 1.2+ x86-64 Details
bitformat-0.9.0-cp311-abi3-musllinux_1_2_ppc64le.whl CPython 3.11 abi3 Linux musl 1.2+ PowerPC 64-le Details
bitformat-0.9.0-cp311-abi3-musllinux_1_2_aarch64.whl CPython 3.11 abi3 Linux musl 1.2+ ARM64 Details
bitformat-0.9.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details
bitformat-0.9.0-cp311-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl CPython 3.11 abi3 Linux glibc 2.17+ PowerPC 64-le Details
bitformat-0.9.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details
bitformat-0.9.0-cp311-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl CPython 3.11 abi3 Linux glibc 2.5+ x86-32, Linux glibc 2.17+ x86-32 Details
bitformat-0.9.0-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details
bitformat-0.9.0-cp311-abi3-macosx_10_12_x86_64.whl CPython 3.11 abi3 macOS 10.12+ x86-64 Details

Total release size: 9.3 MB

Release files / bitformat-0.9.0.tar.gz

Download URL bitformat-0.9.0.tar.gz
Size 3.7 MB
Tags Source
SHA-256 checksum
How to use checksums
a58e185a512b8ef1dea79512584e3f64d2b56deffccd6e6a0a17340bc328404f
BLAKE2b-256 checksum
How to use checksums
eee420f92ba853c6e5a70dc49c88c4342c958352d2128108742a0ca5a0b65dde
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp314-cp314t-win_amd64.whl

Download URL bitformat-0.9.0-cp314-cp314t-win_amd64.whl
Size 335.7 kB
Tags CPython 3.14 CPython 3.14 free-threading Windows x86-64
SHA-256 checksum
How to use checksums
b072ab2751775e63b5694c20c324ea1351aec15525fea121ace01439873798e3
BLAKE2b-256 checksum
How to use checksums
83a36ecad96adeaa2632424c79a34eadefcf9de0a69df4e1ce636ef2e841fa7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp314-cp314t-win32.whl

Download URL bitformat-0.9.0-cp314-cp314t-win32.whl
Size 318.8 kB
Tags CPython 3.14 CPython 3.14 free-threading Windows x86-32
SHA-256 checksum
How to use checksums
4e20917d57c67449b482b2cae687f824873b6d870d41c2eb76c5b41d2d13f9c5
BLAKE2b-256 checksum
How to use checksums
8489743506851bff4c19e910343bb59a0bbe1391698d3613cd0d0052e69afcf4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp311-abi3-win_amd64.whl

Download URL bitformat-0.9.0-cp311-abi3-win_amd64.whl
Size 332.9 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
6c912ab302435f94dc2260bc8eebd29250404de7823d5fb7af1ff25c8f755f91
BLAKE2b-256 checksum
How to use checksums
4f2e8d7c9f1bdda3c8291cc1a6d38f8db859c925ed99abac972677bfb0fccd22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp311-abi3-win32.whl

Download URL bitformat-0.9.0-cp311-abi3-win32.whl
Size 318.4 kB
Tags CPython 3.11 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
b630046257e2083cf4bff60dde2bf3d50214ab62b9d3bacb514bea21cb8f6e5a
BLAKE2b-256 checksum
How to use checksums
253521ffd7ad6672f51a79491e412134ec0362e28978ce1e8acc52f583ba154c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp311-abi3-musllinux_1_2_x86_64.whl

Download URL bitformat-0.9.0-cp311-abi3-musllinux_1_2_x86_64.whl
Size 540.2 kB
Tags CPython 3.11 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
cb7fc5987d9448e7dca953a38fcd1d1338bf4ecf8ff26684a09de176aa1948a3
BLAKE2b-256 checksum
How to use checksums
6bb70d96e2f67978823bbaa527f71c5e7757492626b56f1ba9c5c0504230d3ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp311-abi3-musllinux_1_2_ppc64le.whl

Download URL bitformat-0.9.0-cp311-abi3-musllinux_1_2_ppc64le.whl
Size 537.6 kB
Tags CPython 3.11 Linux musl 1.2+ PowerPC 64-le abi3
SHA-256 checksum
How to use checksums
fd9590209e37b82f27f672886fa96dc221d934b56f63b03e586abb5c1e89fb71
BLAKE2b-256 checksum
How to use checksums
2d293991186af8b6b4f2ea694fb4fde2073bdae4ef0c57533bae4a2670b35ebf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp311-abi3-musllinux_1_2_aarch64.whl

Download URL bitformat-0.9.0-cp311-abi3-musllinux_1_2_aarch64.whl
Size 511.7 kB
Tags CPython 3.11 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
4fae0990743b6ddf21b69fd6ff11f6beaf569d6cb3c0ce40a4703140106fb5d2
BLAKE2b-256 checksum
How to use checksums
ae71be640210a1b48e8477a4d9f80409589f3fdaeee2332d8e20da78428d7495
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL bitformat-0.9.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 458.0 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
e4fc05a96765eae303a479f2f91f4b7cde4846679b0929467ef4533c1060f0a8
BLAKE2b-256 checksum
How to use checksums
cae9c7fa13fd8923afae3c654b1fcaf92f88fd8812a8c690d08e71bae306d33d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp311-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl

Download URL bitformat-0.9.0-cp311-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl
Size 495.5 kB
Tags CPython 3.11 Linux glibc 2.17+ PowerPC 64-le abi3
SHA-256 checksum
How to use checksums
560f8c327fba15fb9a578d5926e96b7814a665bf0461684ded769f56967cbc29
BLAKE2b-256 checksum
How to use checksums
78d3542b0564451f5e9e2d644b2754342d7e70442ee669349b2340de89321855
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL bitformat-0.9.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 446.7 kB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
e784925d5e2d8dda54e4172f41547444e0dd249bcc45cefb13351ead6335b320
BLAKE2b-256 checksum
How to use checksums
5bea29e433d554e87e5a00b6a13c3f2f2283ac535bddbd21903005cfba3a3468
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp311-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl

Download URL bitformat-0.9.0-cp311-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl
Size 485.8 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-32 Linux glibc 2.5+ x86-32 abi3
SHA-256 checksum
How to use checksums
2368fcacb79703cf40cf35f1f37b3fea593e2ec3b227dc70cb443ae8e2987cc8
BLAKE2b-256 checksum
How to use checksums
976165af9e08b3becbe9047a5edb44d8f758ac78b1f5157e45ac2b71e9260772
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp311-abi3-macosx_11_0_arm64.whl

Download URL bitformat-0.9.0-cp311-abi3-macosx_11_0_arm64.whl
Size 416.9 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
add22ad080307d74cd115c38eee0d4af7e01869f35faf29e4aab3185b3025f5f
BLAKE2b-256 checksum
How to use checksums
06c9b6a2504dec56ab741bb0dab7e7270f4906b495f0c292b732d9391f90f8d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / bitformat-0.9.0-cp311-abi3-macosx_10_12_x86_64.whl

Download URL bitformat-0.9.0-cp311-abi3-macosx_10_12_x86_64.whl
Size 446.5 kB
Tags CPython 3.11 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
4e8eab73e2b0f09445c804bca38d831ad635e846101290c109af90e766bc83c8
BLAKE2b-256 checksum
How to use checksums
29924f29c9ad1016ffa7226e520a2234efb68b7219afc98a3d97566fb68cd99b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7
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