Skip to main content

bitstring

bitstring is a Python library to help make the creation and analysis of all types of bit-level binary data as simple and efficient as possible. It has been actively maintained since 2006.

CI badge Docs Codacy Badge Dependents (via libraries.io)     Pepy Total Downloads PyPI - Downloads


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

News

I'm rewriting the core of bitstring in Rust, and have released that cut-down and streamlined interface as the tibs package. It's now in beta, and feedback is welcome. Just pip install tibs to try it out.

tibs

A sleek Python library for your binary data

Overview

  • Efficiently store and manipulate binary data in idiomatic Python.
  • Create bitstrings from hex, octal, binary, files, formatted strings, bytes, integers and floats of different endiannesses.
  • Powerful binary packing and unpacking functions.
  • Bit-level slicing, joining, searching, replacing and more.
  • Create and manipulate arrays of fixed-length bitstrings.
  • Read from and interpret bitstrings as streams of binary data.
  • Rich API - chances are that whatever you want to do there's a simple and elegant way of doing it.
  • Open source software, released under the MIT licence.

Documentation

Extensive documentation for the bitstring library is available. Some starting points are given below:

There is also an introductory walkthrough notebook on binder.

Examples

Installation

$ pip install bitstring

Creation

>>> from bitstring import Bits, BitArray, BitStream, pack
>>> a = BitArray(bin='00101')
>>> b = Bits(a_file_object)
>>> c = BitArray('0xff, 0b101, 0o65, uint6=22')
>>> d = pack('intle16, hex=a, 0b1', 100, a='0x34f')
>>> e = pack('<16h', *range(16))

Different interpretations, slicing and concatenation

>>> a = BitArray('0x3348')
>>> a.hex, a.bin, a.uint, a.float, a.bytes
('3348', '0011001101001000', 13128, 0.2275390625, b'3H')
>>> a[10:3:-1].bin
'0101100'
>>> '0b100' + 3*a
BitArray('0x866906690669, 0b000')

Reading data sequentially

>>> b = BitStream('0x160120f')
>>> b.read(12).hex
'160'
>>> b.pos = 0
>>> b.read('uint12')
352
>>> b.readlist('uint12, bin3')
[288, '111']

Searching, inserting and deleting

>>> c = BitArray('0b00010010010010001111')   # c.hex == '0x1248f'
>>> c.find('0x48')
(8,)
>>> c.replace('0b001', '0xabc')
>>> c.insert('0b0000', pos=3)
>>> del c[12:16]

Arrays of fixed-length formats

>>> from bitstring import Array
>>> a = Array('uint7', [9, 100, 3, 1])
>>> a.data
BitArray('0x1390181')
>>> a[::2] *= 5
>>> a
Array('uint7', [45, 100, 15, 1])

Copyright (c) 2006 - 2026 Scott Griffiths

Metadata

Release files for bitstring 4.4.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 bitstring 4.4.0
File Size Uploaded
bitstring-4.4.0.tar.gz 255.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bitstring 4.4.0
File Interpreter ABI Platform
bitstring-4.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 332.1 kB

Release files / bitstring-4.4.0.tar.gz

Download URL bitstring-4.4.0.tar.gz
Size 255.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e682ac522bb63e041d16cbc9d0ca86a4f00194db16d0847c7efe066f836b2e37
BLAKE2b-256 checksum
How to use checksums
36d3de6fe4e7065df8c2f1ac1766f5fdccbe75bc18af2cf2dbeecd34d68e1518
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / bitstring-4.4.0-py3-none-any.whl

Download URL bitstring-4.4.0-py3-none-any.whl
Size 76.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
feac49524fcf3ef27e6081e86f02b10d2adf6c3773bf22fbe0e7eea9534bc737
BLAKE2b-256 checksum
How to use checksums
bf021a870bab76f2896d827aa4963be95e56675ffa1453e53525d13c43036edf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

4.4.0 This release

2 release files

4.3.1

2 release files

4.3.0

2 release files

4.2.3

2 release files

4.2.2

2 release files

4.2.1

2 release files

4.2.0

2 release files

4.1.4

2 release files

4.1.3

2 release files

4.1.2

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.1.9

3 release files

3.1.8

3 release files

3.1.7

1 release file

3.1.6

3 release files

3.1.5

1 release file

3.1.4

1 release file

3.1.3

3.1.2

1 release file

3.1.1

1 release file

3.1.0

1 release file

3.0.2

1 release file

3.0.1

1 release file

3.0.0

1 release file

2.2.0

1 release file

2.1.1

1 release file

2.1.0

1 release file

2.0.3

1 release file

2.0.2

1 release file

1.3.0

1 release file

1.2.0

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.0

1 release file

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.3

1 release file

0.4.2

1 release file

0.4.0

2 release files

0.3.2

1 release file

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