bitarray

raw JSON →
2.3.8 verified Tue May 12 auth: no python install: verified

Efficient boolean arrays implemented as a C extension for Python. Provides bit-level operations, packed representations, and performance comparable to NumPy for bit manipulation. Current version 2.3.8. Released under PSF license.

pip install bitarray
error TypeError: 'bitarray' object is not callable
cause Trying to call a bitarray object as a function, e.g., a() instead of a[index].
fix
Use indexing to access bits: bit = a[0]
error ValueError: bitarray must not contain whitespace
cause Constructing bitarray from a string that contains spaces or other invalid characters.
fix
Remove whitespace: bitarray('11001010') not bitarray('1100 1010')
error ImportError: cannot import name 'bitarray' from 'bitarray'
cause Incorrect import attempt or missing installation/conflict with another package named bitarray.
fix
Use 'from bitarray import bitarray'. If you already installed, check for version conflicts: 'pip install --upgrade bitarray'
breaking bitarray objects are not hashable. Attempting to use them as dictionary keys or in sets will raise TypeError.
fix Convert to bytes or tuple for hashing, e.g., key = a.tobytes()
deprecated The 'bitarray.endian()' method is deprecated in favor of the 'endian' property in version 2.0+.
fix Use 'bitarray.endian' (property) instead of 'bitarray.endian()' (method).
gotcha When creating a bitarray from an integer, the integer is treated as the length (number of bits), not as the initial bits. bitarray(8) creates 8 zero bits, not a bitarray with one bit set at position 3.
fix Use bitarray('111') for specific bits or bitarray(8) for an all-zero array of given length.
gotcha The 'count()' method with 'value' parameter default is 1. bitarray('110').count() returns 2 (counts 1s). Use count(0) to count zeros.
fix Explicitly pass value=0 for zero count: mybitarray.count(0).
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.00s 19.0M
3.10 alpine (musl) - - 0.00s 19.0M
3.10 slim (glibc) wheel 2.1s 0.00s 20M
3.10 slim (glibc) - - 0.00s 20M
3.11 alpine (musl) wheel - 0.00s 21.2M
3.11 alpine (musl) - - 0.01s 21.2M
3.11 slim (glibc) wheel 1.9s 0.00s 22M
3.11 slim (glibc) - - 0.00s 22M
3.12 alpine (musl) wheel - 0.00s 13.0M
3.12 alpine (musl) - - 0.00s 13.0M
3.12 slim (glibc) wheel 1.7s 0.00s 14M
3.12 slim (glibc) - - 0.00s 14M
3.13 alpine (musl) wheel - 0.00s 12.8M
3.13 alpine (musl) - - 0.00s 12.6M
3.13 slim (glibc) wheel 1.7s 0.00s 13M
3.13 slim (glibc) - - 0.00s 13M
3.9 alpine (musl) wheel - 0.00s 18.5M
3.9 alpine (musl) - - 0.00s 18.5M
3.9 slim (glibc) wheel 2.4s 0.00s 19M
3.9 slim (glibc) - - 0.00s 19M

Create bitarrays, perform bitwise operations, and convert to bytes.

from bitarray import bitarray
# Create a bitarray of 8 zero bits
a = bitarray(8)
print('Length:', len(a))  # 8
# Set bits from a binary string
a = bitarray('11001010')
print(a)  # bitarray('11001010')
# Bitwise operations
b = bitarray('11110000')
c = a & b
print(c)  # bitarray('11000000')
# Convert to bytes
packed = a.tobytes()
print('Bytes:', packed)