Search code examples
pythonbit-fieldsbitarray

Does Python have a bitfield type?


I need a compact representation of an array of booleans, does Python have a builtin bitfield type or will I need to find an alternate solution?


Solution

  • Bitarray was the best answer I found, when I recently had a similar need. It's a C extension (so much faster than BitVector, which is pure python) and stores its data in an actual bitfield (so it's eight times more memory efficient than a numpy boolean array, which appears to use a byte per element.)