Skip to main content
/doc/bitstring_logo_small.png

bitstring is a pure Python module designed to help make the creation and analysis of binary data as simple and natural as possible.

Bitstrings can be constructed from integers (big and little endian), hex, octal, binary, strings or files. They can be sliced, joined, reversed, inserted into, overwritten, etc. with simple functions or slice notation. They can also be read from, searched and replaced, and navigated in, similar to a file or stream.

bitstring is open source software, and has been released under the MIT licence.

This module works in both Python 2.7 and Python 3.

Installation

Probably all you need to do is:

pip install bitstring

Alternatively if you have downloaded and unzipped the package then you need to run the setup.py script with the ‘install’ argument:

python setup.py install

You may need to run this with root privileges on Unix-like systems.

Documentation

The manual for the bitstring module is available here <http://packages.python.org/bitstring>. It contains a walk-through of all the features and a complete reference section.

It is also available as a PDF as part of the source download.

Simple Examples

Creation:

>>> a = BitArray(bin='00101')
>>> b = Bits(a_file_object)
>>> c = BitArray('0xff, 0b101, 0o65, uint:6=22')
>>> d = pack('intle:16, hex=a, 0b1', 100, a='0x34f')
>>> e = pack('<16h', *range(16))

Different interpretations, slicing and concatenation:

>>> a = BitArray('0x1af')
>>> a.hex, a.bin, a.uint
('1af', '000110101111', 431)
>>> a[10:3:-1].bin
'1110101'
>>> 3*a + '0b100'
BitArray('0o0657056705674')

Reading data sequentially:

>>> b = BitStream('0x160120f')
>>> b.read(12).hex
'160'
>>> b.pos = 0
>>> b.read('uint:12')
352
>>> b.readlist('uint:12, bin:3')
[288, '111']

Searching, inserting and deleting:

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

Unit Tests

The 400+ unit tests should all pass for Python 2.7 and later. To run them, from the test directory run:

python -m unittest discover

The bitstring module has been released as open source under the MIT License. Copyright (c) 2019 Scott Griffiths

For more information see the project’s homepage on GitHub: <https://github.com/scott-griffiths/bitstring>

Release files for bitstring 3.1.6

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 3.1.6
File Size Uploaded
bitstring-3.1.6.tar.gz 645.0 kB Details

Built distributions (wheels)

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

Total release size: 719.2 kB

Release files / bitstring-3.1.6.tar.gz

Download URL bitstring-3.1.6.tar.gz
Size 645.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c97a8e2a136e99b523b27da420736ae5cb68f83519d633794a6a11192f69f8bf
BLAKE2b-256 checksum
How to use checksums
f487fe6f7093088f4396e84c1b5d5dfb08c4840487ad46ff4805b7008c8f5ffc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

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

Download URL bitstring-3.1.6-py3-none-any.whl
Size 37.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b60b0c300d0d3d0a24ec84abfda4b0eaed3dc56dc90f6cbfe497166c9ad8443
BLAKE2b-256 checksum
How to use checksums
c7b9ce7458b35633968eae3477a201a00f6c8e01cab0ccb200ea5099d41dd977
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

Release files / bitstring-3.1.6-py2-none-any.whl

Download URL bitstring-3.1.6-py2-none-any.whl
Size 37.1 kB
Tags Python 2
SHA-256 checksum
How to use checksums
e392819965e7e0246e3cf6a51d5a54e731890ae03ebbfa3cd0e4f74909072096
BLAKE2b-256 checksum
How to use checksums
c17520022e089ac83afdf448b1dbf2e7dfce3cd68d28cfb973440e23b32c2540
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

Release history Release notifications | RSS feed

4.4.0

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

This release

3.1.6 This release

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