
cbits
High-performance BitVector for Python.
Description
cbits implements a powerful bit vector type in C that is accessible via a Python API. It stores bits contiguously in memory and minimizes overhead compared to a list of Boolean values. Core operations such as setting, clearing, flipping and counting bits run in constant or near-constant time per word, making it ideal for extensive bit manipulation.
Installation
Quick Start
from cbits import BitVector
bv.set(3)
bv.clear(3)
bv.flip(0)
print(bv.get(0))
print(len(bv), bv.bits)
bv.set(-1)
print(bv.get(63))
print(bool(bv))
print(bv.rank(32))
import copy
bv2 = bv.copy()
bv3 = copy.deepcopy(bv)
a.set(0); a.set(2)
b.set(1); b.set(2)
print((a & b)[0], (a | b)[0], (a ^ b)[0], (~a)[0])
bv[5] = True
print(bv[5])
print([i for i, bit in enumerate(bv) if bit])
sv = bv[2:10]
print([sv.get(i) for i in range(len(sv))])
sv2 = bv[:5]
sv3 = bv[5:]
sv4 = bv[::2]
sv_rev = bv[::-1]
print([sv4.get(i) for i in range(len(sv4))])
print([sv_rev.get(i) for i in range(len(sv_rev))])
bv[0:4] = [True, False, True, False]
print([bv.get(i) for i in range(4)])
print([bv.get(i) for i in range(4, 8)])
bv[9:5:-1] = (i % 2 == 0 for i in range(4))
print([bv.get(i) for i in range(9, 5, -1)])
a.set(0); a.set(2); a.set(5)
b.set(0); b.set(3)
print(b in a)
Packed array of bits with support for rank/select operations.
Definition bitvector.h:64
API Reference
Class: BitVector
def __init__(self, size: int)
@property
def bits(self) -> int
def get(self, index: int) -> bool
def set(self, index: int) -> None
def clear(self, index: int) -> None
def flip(self, index: int) -> None
def rank(self, index: int) -> int
def copy(self) -> BitVector
def __copy__(self) -> BitVector
def __deepcopy__(self, memo) -> BitVector
def __len__(self) -> int
def __getitem__(self, index: int) -> bool
def __setitem__(self, index: int, value: bool) -> None
def __getiten(self, s: slice) -> BitVector
def __setitem(self, s: slice, value: Iterable[bool]) -> None
def __contains__(self, other: BitVector) -> bool
def __iter__(self) -> Iterator[bool]
def __and__(self, other: BitVector) -> BitVector
def __iand__(self, other: BitVector) -> BitVector
def __or__(self, other: BitVector) -> BitVector
def __ior__(self, other: BitVector) -> BitVector
def __xor__(self, other: BitVector) -> BitVector
def __ixor__(self, other: BitVector) -> BitVector
def __invert__(self) -> BitVector
def __bool__(self) -> bool
def __repr__(self) -> str
def __str__(self) -> str
License
Apache License 2.0 See LICENSE for details.
Project Links