Compact bit vector with set operations, slicing, and efficient storage for Python developers.
From source:
pip install -e /tmp/bitvecOr from PyPI (once published):
pip install bitvecfrom bitvec import BitVec
# Create a bit vector
bv = BitVec(8)
bv.set(0)
bv.set(3)
bv.set(7)
# Check bits
print(bv.get(3)) # True
print(bv.count()) # 3
# Bitwise operations
bv2 = BitVec(8)
bv2.set(2)
bv2.set(3)
result = bv & bv2 # AND operation
print(result.count()) # 1 (only bit 3 is in both)
# Iterate over set bits
for idx in bv:
print(idx) # 0, 3, 7
# Slicing
bits = bv[2:6] # Get bits 2-5
bv[2:5] = True # Set bits 2-4
# Convert to bytes
data = bv.to_bytes()
bv_restored = BitVec.from_bytes(data, len(bv))- Efficient storage: Uses one byte per 8 bits
- Full bitwise operations: AND, OR, XOR, NOT
- Flexible creation: From size, bytes, or iterable of indices
- Slicing support: Get/set ranges of bits with Python slicing syntax
- Iterator protocol: Iterate over set bit indices
- Type-safe: Full type hints for static analysis
- Tested: Comprehensive test coverage (80+ tests)
BitVec(size: int) -> BitVecCreate a zero-initialized bit vector of given size.
BitVec.from_bytes(data: bytes, size: int) -> BitVecCreate a bit vector from raw bytes. Raises ValueError if data is too small.
BitVec.from_iterable(indices: Iterable[int], size: int) -> BitVecCreate a bit vector with specified bit indices set. Raises IndexError if any index is out of range.
get(index: int) -> boolGet the value of bit at index (True=1, False=0).
set(index: int) -> NoneSet bit at index to 1.
clear(index: int) -> NoneClear bit at index (set to 0).
toggle(index: int) -> NoneToggle bit at index (flip 0→1 or 1→0).
bv[index: int] -> bool
bv[index: int] = value: boolGet/set single bits using bracket notation.
bv[start:stop:step] -> List[bool]
bv[start:stop:step] = value: Union[bool, List[bool]]Get/set ranges of bits with full Python slice support.
bv1 & bv2 -> BitVec # AND
bv1 | bv2 -> BitVec # OR
bv1 ^ bv2 -> BitVec # XOR
~bv -> BitVec # NOT (invert)All bitwise operations raise ValueError if vector sizes differ (except NOT).
count() -> intReturn number of set bits (popcount).
any() -> boolReturn True if any bit is set.
all() -> boolReturn True if all bits are set (vacuous truth for empty vectors).
none() -> boolReturn True if no bits are set.
__len__() -> intReturn the size of the vector.
__iter__() -> Iterator[int]Iterate over indices of set bits in ascending order.
to_bytes() -> bytesConvert to bytes representation.
__bool__() -> boolReturn True if any bit is set (same as any()).
__eq__(other: BitVec) -> boolCheck equality with another bit vector.
__repr__() -> strReturn string representation like "BitVec(size=8, count=3)".
BitVecErrorRaised for various BitVec errors (index out of range, type errors, size mismatches).
- IndexError / BitVecError: Index out of range
- TypeError / BitVecError: Non-integer index or wrong value type
- ValueError / BitVecError: Size mismatch for bitwise operations or negative size
- Empty bit vectors (size=0)
- Non-byte-aligned sizes (e.g., 13 bits)
- Very large vectors (10,000+ bits)
- All bitwise operations on different-sized vectors (raise error)
- Inverted non-aligned vectors (extra bits properly masked)
- Duplicate indices in from_iterable (idempotent)
- Slicing with negative indices and large steps
cd /tmp/bitvec
python -m pytest tests/ -vTest coverage:
- test_construction.py: 15 tests (creation from various sources)
- test_access.py: 18 tests (get, set, clear, toggle, indexing, slicing)
- test_bitwise.py: 19 tests (AND, OR, XOR, NOT operations)
- test_iteration.py: 20 tests (len, iter, bool, equality, any/all/none)
- test_edges.py: 25 tests (edge cases, roundtrips, type checking)
Total: 97 tests, all passing
- O(1) single-bit access
- O(n) popcount (where n = number of bytes)
- O(n) for bitwise operations (where n = number of bytes)
- O(k) for iteration (where k = number of set bits)
- O(1) memory per bit (1 bit per bit, rounded to byte)
- Uses Python's
bytearrayfor efficient storage - Bits are stored in little-endian order within bytes
- Thread-safe for read-only operations, not thread-safe for concurrent modifications
- Pure Python implementation, no external dependencies
MIT License - see LICENSE file for details.
Nripanka Das nripankadas@gmail.com