A Python module to help you manage your bits
See the code
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.
[!NOTE] To see what's been added, improved or fixed, and also to see what's coming in the next version, see the release notes.
[!IMPORTANT] The
mainbranch now contains the upcoming bitstring 5.0 work. It is visible here for testing and feedback, but it is not the current released version. The released PyPI line is still bitstring 4.x; usepip install bitstringor pinbitstring<5for the stable API. Source maintenance for 4.x is on the4.x-maintenancebranch.
Version 5.0 of bitstring is in development. This is a major update with breaking changes.
Reasons to upgrade include:
Bits and BitArray store data, while Reader stores the current bit position.bitarray dependency, LSB0 mode and the old stream classes.Version 5 is worth moving to if you need the improved performance, want the cleaner current API, or want to keep up to date with future bitstring development. The changes are quite broad, so if your existing code is stable and performance is not a problem, there is no need to migrate immediately.
For details on the source changes you may need, see the Upgrading to version 5 guide.
bitstring 5 is built on tibs, a simpler and more focussed Python library for binary data, written in Rust for speed. It's by the same author as bitstring so they can complement each other's needs. If you want a leaner interface and do not need all of bitstring's higher-level format handling and historical API, tibs may be a better fit for new code.
Tibs has now reached its 2.0 release. Please do try it, as all feedback is welcome.
A sleek Python library for binary data
Reader to read from and interpret bitstrings sequentially.Extensive documentation for the bitstring library is available. Some starting points are given below:
There is also an introductory walkthrough notebook for the 5.0 branch on binder.
These examples show the upcoming 5.0 API from main.
>>> from bitstring import Bits, BitArray, Reader, pack
>>> a = BitArray(bin='00101')
>>> b = Bits.from_file(a_file_object)
>>> c = BitArray('0xff, 0b101, 0o65, u6=22')
>>> d = pack('ile16, hex=a, 0b1', 100, a='0x34f')
>>> e = pack('<16h', *range(16))
>>> a = BitArray('0x3348')
>>> a.hex, a.bin, a.u, a.f, a.bytes
('3348', '0011001101001000', 13128, 0.2275390625, b'3H')
>>> a[10:3:-1].bin
'0101100'
>>> '0b100' + 3*a
BitArray('0x866906690669, 0b000')
>>> b = Reader(Bits('0x160120f'))
>>> b.read_bits(12).hex
'160'
>>> b.pos = 0
>>> b.read_value('u12')
352
>>> b.read_list('u12, bin3')
[288, '111']
>>> c = BitArray('0b00010010010010001111') # c.hex == '0x1248f'
>>> c.find('0x48')
8
>>> c.replace('0b001', '0xabc')
5
>>> c.insert(3, '0b0000')
>>> del c[12:16]
>>> from bitstring import Array
>>> a = Array('u7', [9, 100, 3, 1])
>>> a.data
BitArray('0x1390181')
>>> a[::2] *= 5
>>> a
Array('u7', [45, 100, 15, 1])
Copyright (c) 2006 - 2026 Scott Griffiths
Overall
Maintained
Code review
PyPI
Latest release
Releases this year
685 followers · starred Aug 2019
24 followers · starred Apr 2016
11 followers · starred Aug 2019
18 followers · starred Mar 2017
A Python module to help you manage your bits
See the code
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.
[!NOTE] To see what's been added, improved or fixed, and also to see what's coming in the next version, see the release notes.
[!IMPORTANT] The
mainbranch now contains the upcoming bitstring 5.0 work. It is visible here for testing and feedback, but it is not the current released version. The released PyPI line is still bitstring 4.x; usepip install bitstringor pinbitstring<5for the stable API. Source maintenance for 4.x is on the4.x-maintenancebranch.
Version 5.0 of bitstring is in development. This is a major update with breaking changes.
Reasons to upgrade include:
Bits and BitArray store data, while Reader stores the current bit position.bitarray dependency, LSB0 mode and the old stream classes.Version 5 is worth moving to if you need the improved performance, want the cleaner current API, or want to keep up to date with future bitstring development. The changes are quite broad, so if your existing code is stable and performance is not a problem, there is no need to migrate immediately.
For details on the source changes you may need, see the Upgrading to version 5 guide.
bitstring 5 is built on tibs, a simpler and more focussed Python library for binary data, written in Rust for speed. It's by the same author as bitstring so they can complement each other's needs. If you want a leaner interface and do not need all of bitstring's higher-level format handling and historical API, tibs may be a better fit for new code.
Tibs has now reached its 2.0 release. Please do try it, as all feedback is welcome.
A sleek Python library for binary data
Reader to read from and interpret bitstrings sequentially.Extensive documentation for the bitstring library is available. Some starting points are given below:
There is also an introductory walkthrough notebook for the 5.0 branch on binder.
These examples show the upcoming 5.0 API from main.
>>> from bitstring import Bits, BitArray, Reader, pack
>>> a = BitArray(bin='00101')
>>> b = Bits.from_file(a_file_object)
>>> c = BitArray('0xff, 0b101, 0o65, u6=22')
>>> d = pack('ile16, hex=a, 0b1', 100, a='0x34f')
>>> e = pack('<16h', *range(16))
>>> a = BitArray('0x3348')
>>> a.hex, a.bin, a.u, a.f, a.bytes
('3348', '0011001101001000', 13128, 0.2275390625, b'3H')
>>> a[10:3:-1].bin
'0101100'
>>> '0b100' + 3*a
BitArray('0x866906690669, 0b000')
>>> b = Reader(Bits('0x160120f'))
>>> b.read_bits(12).hex
'160'
>>> b.pos = 0
>>> b.read_value('u12')
352
>>> b.read_list('u12, bin3')
[288, '111']
>>> c = BitArray('0b00010010010010001111') # c.hex == '0x1248f'
>>> c.find('0x48')
8
>>> c.replace('0b001', '0xabc')
5
>>> c.insert(3, '0b0000')
>>> del c[12:16]
>>> from bitstring import Array
>>> a = Array('u7', [9, 100, 3, 1])
>>> a.data
BitArray('0x1390181')
>>> a[::2] *= 5
>>> a
Array('u7', [45, 100, 15, 1])
Copyright (c) 2006 - 2026 Scott Griffiths
Overall
Maintained
Code review
PyPI
Latest release
Releases this year
685 followers · starred Aug 2019
24 followers · starred Apr 2016
11 followers · starred Aug 2019
18 followers · starred Mar 2017