CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-bitarray

Efficient arrays of booleans with comprehensive sequence operations, bitwise operations, and specialized functionality for encoding/decoding variable-length prefix codes.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

utility-functions.mddocs/

Utility Functions

Extended functionality from the bitarray.util module including analysis functions, pretty printing, set operations, serialization, and specialized bit manipulation operations.

Capabilities

Analysis Functions

Functions for analyzing bitarray contents and computing statistical properties.

def count_n(a: bitarray, n: int, value: int = 1) -> int:
    """
    Count non-overlapping occurrences of n consecutive bits with specified value.
    
    Args:
        a: Input bitarray
        n: Length of bit sequence to count
        value: Bit value to count (0 or 1)
        
    Returns:
        Number of non-overlapping occurrences
    """

def parity(a: bitarray) -> int:
    """
    Calculate parity (XOR of all bits).
    
    Args:
        a: Input bitarray
        
    Returns:
        0 if even number of 1-bits, 1 if odd
    """

def sum_indices(a: bitarray, mode: int = 1) -> int:
    """
    Sum indices of set bits.
    
    Args:
        a: Input bitarray
        mode: Calculation mode (1 for standard sum)
        
    Returns:
        Sum of all indices where bit is set
    """

def xor_indices(a: bitarray) -> int:
    """
    Calculate XOR of all indices where bit is set.
    
    Args:
        a: Input bitarray
        
    Returns:
        XOR of all set bit indices
    """

Usage Examples:

from bitarray import bitarray
from bitarray.util import count_n, parity, sum_indices, xor_indices

a = bitarray('11010110')

# Analysis functions
consecutive_1s = count_n(a, 2, 1)    # Count '11' patterns: 1
consecutive_0s = count_n(a, 1, 0)    # Count '0' bits: 3
parity_val = parity(a)               # XOR of all bits: 0
index_sum = sum_indices(a)           # Sum of indices with 1s
index_xor = xor_indices(a)           # XOR of indices with 1s

Set Operations

Functions for performing set-like operations between bitarrays.

def count_and(a: bitarray, b: bitarray) -> int:
    """
    Count bits set in both bitarrays (intersection).
    
    Args:
        a, b: Input bitarrays
        
    Returns:
        Number of bits set in both a and b
    """

def count_or(a: bitarray, b: bitarray) -> int:
    """
    Count bits set in either bitarray (union).
    
    Args:
        a, b: Input bitarrays
        
    Returns:
        Number of bits set in a or b (or both)
    """

def count_xor(a: bitarray, b: bitarray) -> int:
    """
    Count bits different between bitarrays (symmetric difference).
    
    Args:
        a, b: Input bitarrays
        
    Returns:
        Number of bits where a and b differ
    """

def any_and(a: bitarray, b: bitarray) -> bool:
    """
    Check if any bits are set in both bitarrays.
    
    Args:
        a, b: Input bitarrays
        
    Returns:
        True if intersection is non-empty
    """

def subset(a: bitarray, b: bitarray) -> bool:
    """
    Check if a is subset of b (all bits in a are also in b).
    
    Args:
        a, b: Input bitarrays
        
    Returns:
        True if a is subset of b
    """

def correspond_all(a: bitarray, b: bitarray) -> tuple:
    """
    Return correspondence information between bitarrays.
    
    Args:
        a, b: Input bitarrays
        
    Returns:
        Tuple with correspondence data
    """

Usage Examples:

from bitarray import bitarray
from bitarray.util import count_and, count_or, count_xor, any_and, subset

a = bitarray('11010')
b = bitarray('10110')

# Set operations
and_count = count_and(a, b)     # 2 (bits set in both)
or_count = count_or(a, b)       # 4 (bits set in either)
xor_count = count_xor(a, b)     # 2 (bits different)

# Boolean tests
has_overlap = any_and(a, b)     # True (some bits match)
is_subset = subset(a, b)        # False (a is not subset of b)

# Check subset relationship
c = bitarray('10010')
d = bitarray('11011')
is_c_subset_d = subset(c, d)    # True (all 1s in c are also in d)

Pretty Printing

Functions for displaying bitarrays in formatted, readable ways.

def pprint(a: Any, 
           stream: Optional[BinaryIO] = None,
           group: int = 8,
           indent: int = 4, 
           width: int = 80) -> None:
    """
    Pretty-print bitarray with formatting options.
    
    Args:
        a: Bitarray or other object to print
        stream: Output stream (stdout if None)
        group: Number of bits per group
        indent: Indentation level
        width: Maximum line width
    """

def strip(a: bitarray, mode: str = 'right') -> bitarray:
    """
    Strip leading/trailing zeros from bitarray.
    
    Args:
        a: Input bitarray
        mode: Strip mode - 'left', 'right', or 'both'
        
    Returns:
        Stripped bitarray
    """

Usage Examples:

from bitarray import bitarray
from bitarray.util import pprint, strip

# Long bitarray for demonstration
a = bitarray('0000110101100110010110110001100011010110100000')

# Pretty printing with grouping
pprint(a, group=4, width=40)

# Strip zeros
b = bitarray('0001101000')
stripped_right = strip(b)           # '0001101' (default right)
stripped_left = strip(b, 'left')    # '1101000'
stripped_both = strip(b, 'both')    # '1101'

Data Processing

Functions for processing and manipulating bit sequences and intervals.

def intervals(a: bitarray) -> Iterator:
    """
    Return iterator of intervals of consecutive set bits.
    
    Args:
        a: Input bitarray
        
    Yields:
        Tuples (start, stop) for each interval of consecutive 1s
    """

def byteswap(a: Union[bytes, bytearray], n: int) -> None:
    """
    Swap byte order in-place for n-byte integers.
    
    Args:
        a: Byte array to modify
        n: Size of integers in bytes (2, 4, or 8)
    """

Usage Examples:

from bitarray import bitarray
from bitarray.util import intervals, byteswap

# Find intervals of consecutive 1s
a = bitarray('0011101001110000111')
interval_list = list(intervals(a))
# [(2, 5), (8, 11), (16, 19)] - ranges of consecutive 1s

# Byte swapping for endianness conversion
data = bytearray(b'\\x12\\x34\\x56\\x78')
byteswap(data, 2)  # Swap 2-byte integers: b'\\x34\\x12\\x78\\x56'

Serialization

Functions for serializing bitarrays to portable byte formats and deserializing them back.

def serialize(a: bitarray) -> bytes:
    """
    Serialize bitarray to bytes with metadata.
    Preserves endianness and exact bit length.
    
    Args:
        a: Input bitarray
        
    Returns:
        Serialized bytes including metadata
    """

def deserialize(b: Union[bytes, bytearray]) -> bitarray:
    """
    Deserialize bytes back to bitarray.
    Restores original endianness and bit length.
    
    Args:
        b: Serialized bytes
        
    Returns:
        Restored bitarray
    """

Usage Examples:

from bitarray import bitarray
from bitarray.util import serialize, deserialize

# Serialization preserves all properties
original = bitarray('1011010', 'little')
serialized = serialize(original)

# Deserialize restores exact bitarray
restored = deserialize(serialized)
print(restored.endian)      # 'little'
print(restored.to01())      # '1011010'
print(original == restored) # True

# Useful for storage and transmission
with open('bitarray.dat', 'wb') as f:
    f.write(serialize(original))

with open('bitarray.dat', 'rb') as f:
    loaded = deserialize(f.read())

Compression

Functions for compressing sparse bitarrays and variable-length encoding.

def sc_encode(a: bitarray) -> bytes:
    """
    Sparse compression encoding for bitarrays with few set bits.
    
    Args:
        a: Input bitarray
        
    Returns:
        Compressed bytes
    """

def sc_decode(stream: Iterable[int]) -> bitarray:
    """
    Sparse compression decoding.
    
    Args:
        stream: Iterable of integers (compressed data)
        
    Returns:
        Decompressed bitarray
    """

def vl_encode(a: bitarray) -> bytes:
    """
    Variable-length encoding.
    
    Args:
        a: Input bitarray
        
    Returns:
        Variable-length encoded bytes
    """

def vl_decode(stream: Iterable[int], endian: Optional[str] = None) -> bitarray:
    """
    Variable-length decoding.
    
    Args:
        stream: Iterable of integers (encoded data)
        endian: Bit-endianness for result
        
    Returns:
        Decoded bitarray
    """

Usage Examples:

from bitarray import bitarray
from bitarray.util import sc_encode, sc_decode, vl_encode, vl_decode

# Sparse compression works well for arrays with few 1s
sparse = bitarray('000000100000001000000000100000')
compressed = sc_encode(sparse)
decompressed = sc_decode(compressed)

print(len(sparse.tobytes()))    # Original size in bytes
print(len(compressed))          # Compressed size (smaller)
print(sparse == decompressed)   # True

# Variable-length encoding
dense = bitarray('11010110' * 100)  # Dense bitarray
vl_compressed = vl_encode(dense)
vl_decompressed = vl_decode(vl_compressed)
print(dense == vl_decompressed)     # True

Advanced Analysis

Additional functions for complex bitarray analysis and manipulation.

def _ssqi(a: bitarray, i: int) -> int:
    """Internal function for subsequence query indexing (advanced use)"""

Note: Some utility functions like _ssqi are internal implementation details and should generally not be used directly in application code.

Error Handling

Utility functions can raise various exceptions:

  • TypeError: Invalid argument types
  • ValueError: Invalid argument values (e.g., negative lengths, invalid bit values)
  • IndexError: Invalid indices or ranges
  • NotImplementedError: Functions requiring newer Python versions
  • MemoryError: Insufficient memory for large operations

Performance Considerations

Most utility functions are implemented in C for optimal performance:

  • Set operations (count_and, count_or, etc.) are highly optimized
  • Conversion functions use efficient algorithms
  • Serialization preserves bit-level accuracy while minimizing space
  • Compression functions are designed for specific use cases (sparse vs dense data)

The utility module extends bitarray functionality while maintaining the same performance characteristics as the core library.

Install with Tessl CLI

npx tessl i tessl/pypi-bitarray

docs

advanced-features.md

core-operations.md

creation-conversion.md

index.md

utility-functions.md

tile.json