### Test bitarray Installation Source: https://github.com/ilanschnell/bitarray/blob/master/README.rst Verify the bitarray installation by running the built-in test suite. This command also displays system information related to the installation. ```shell python -c 'import bitarray; bitarray.test()' ``` -------------------------------- ### Made all examples Py3k compatible Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst Ensures all provided examples are compatible with Python 3. ```python made all examples Py3k compatible ``` -------------------------------- ### Add utils module to examples Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst A new utility module has been added to the examples directory. ```python add utils module to examples ``` -------------------------------- ### JSONizing Bitarrays Example Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst An example demonstrating how to serialize bitarrays into JSON format. ```python extend_json.py ``` -------------------------------- ### Hexadecimal Conversion Example Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst An example showcasing how to convert between hexadecimal strings and bitarrays. ```python hexadecimal.py ``` -------------------------------- ### Lexicographical Permutations Example Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst An example demonstrating how to generate lexicographical permutations using bitarrays. ```python lexico.py ``` -------------------------------- ### Python Example: Tricks Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst This example demonstrates various 'tricks' or advanced usage patterns with bitarrays, likely involving bitwise operations and manipulations. It is part of the bitarray library's development examples. ```python from bitarray import bitarray from bitarray import util # tricks b = bitarray('10101010') print(util.ba2hex(b)) print(util.ba2base(b, 3)) print(util.ba2base(b, 64)) print(util.ba2int(b)) print(util.int2ba(170)) print(util.int2ba(170, 16)) print(util.int2ba(170, 16, 'big')) print(util.int2ba(170, 16, 'little')) print(util.int2ba(170, 16, 'natural')) print(util.int2ba(170, 16, 'natural', True)) print(util.int2ba(170, 16, 'natural', False)) ``` -------------------------------- ### Python Example: Dynamically Growing Sieve Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst This example demonstrates a dynamically growing sieve, a technique for generating prime numbers efficiently. It is part of the bitarray library's examples. ```python import bitarray from bitarray import util # dynamically growing sieve sieve = bitarray.bitarray('0' * 100000) sieve[0:2] = '11' for i in range(2, int(sieve.search("0")[0]**0.5)): if sieve[i] == '0': sieve[i*i::i] = '1' print(util.ba2int(sieve.search("0"))) ``` -------------------------------- ### Add compress.py Example Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst The examples/compress.py script was added to demonstrate compression techniques using bitarrays. ```python add examples/compress.py ``` -------------------------------- ### Update smallints and sieve example to use new utility module Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst The smallints and sieve examples have been updated to utilize the new utility module. ```python update smallints and sieve example to use new utility module ``` -------------------------------- ### Install bitarray Source: https://github.com/ilanschnell/bitarray/blob/master/README.rst Install the bitarray package using pip. Wheels are available on PyPI for major platforms and Python versions. ```shell pip install bitarray ``` -------------------------------- ### Use priority queue for Huffman tree example Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst The Huffman tree example now utilizes a priority queue for improved efficiency. ```python use a priority queue for Huffman tree example (thanks to Ushma Bhatt) ``` -------------------------------- ### Add Bloom Filter Example Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst A Bloom filter example was added to demonstrate its usage with bitarrays. ```python add Bloom filter example ``` -------------------------------- ### Add Gene Sequence Example Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst A gene sequence example was added to showcase bitarray functionality. ```python add gene sequence example ``` -------------------------------- ### Move Huffman code examples Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst All Huffman code related examples have been moved to the examples/huffman directory. ```python move all Huffman code related example code into examples/huffman ``` -------------------------------- ### Python Example: Dubner's Conjecture Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst This example relates to Dubner's conjecture and its implementation using bitarrays. It is part of the bitarray library's examples. ```python import bitarray from bitarray import util # Dubner's conjecture def dubner(n): if n < 2: return False s = util.gen_primes(n) return s[n] print(dubner(1000000)) ``` -------------------------------- ### Python Example: Masked Indexing Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst This example showcases masked indexing in bitarrays, allowing for selective access and modification of bits based on a mask. It is part of the bitarray library's examples. ```python from bitarray import bitarray # masked indexing b = bitarray('10101010') mask = bitarray('11001100') b &= mask print(b) ``` -------------------------------- ### Simplify mandel example to use numba Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst The mandel example has been simplified by integrating the numba library for performance improvements. ```python simplified mandel example to use numba ``` -------------------------------- ### Add Huffman Tree Graphviz .dot File Generation Example Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst This snippet was added to generate a graphviz .dot file of the Huffman tree, located in the examples/huffman directory. ```python add code to generate graphviz .dot file of Huffman tree to examples ``` -------------------------------- ### Run Tests Source: https://github.com/ilanschnell/bitarray/blob/master/examples/puff/README.txt Execute the test suite for the project. Ensure bitarray is installed in your Python 3 environment. ```bash make test ``` -------------------------------- ### Hamming Code Example Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst Demonstrates the usage of Hamming codes, likely for error detection and correction. This example is part of the library's examples. ```python import doctest from bitarray import bitarray def hamming_distance(a, b): """ Calculate the Hamming distance between two bitarrays. >>> hamming_distance(bitarray('1011101'), bitarray('1001001')) 2 >>> hamming_distance(bitarray('1011101'), bitarray('1011101')) 0 """ return (a ^ b).count() def hamming_code(bitarray_in): """ Calculate the Hamming code for a given bitarray. >>> hamming_code(bitarray('1011')) bitarray('10111011') >>> hamming_code(bitarray('0000')) bitarray('00000000') >>> hamming_code(bitarray('1111')) bitarray('11111111') """ n = len(bitarray_in) k = 0 while 2**k < n + k + 1: k += 1 # k parity bits bitarray_out = bitarray(n + k) j = 0 p = 0 for i in range(n + k): if (i + 1) & i == 0: # i+1 is a power of 2 # parity bit bitarray_out[i] = 0 p += 1 else: bitarray_out[i] = bitarray_in[j] j += 1 for i in range(k): p = 1 << i s = 0 for j in range(n + k): if (j + 1) & p: s ^= bitarray_out[j] bitarray_out[p - 1] = s return bitarray_out def hamming_decode(bitarray_in): """ Decode a bitarray with Hamming code. >>> hamming_decode(bitarray('10111011')) bitarray('1011') >>> hamming_decode(bitarray('00000000')) bitarray('0000') >>> hamming_decode(bitarray('11111111')) bitarray('1111') >>> hamming_decode(bitarray('11101011')) # error at pos 3 bitarray('1011') >>> hamming_decode(bitarray('01111011')) # error at pos 1 bitarray('1011') >>> hamming_decode(bitarray('10111001')) # error at pos 7 bitarray('1011') """ n = len(bitarray_in) k = 0 while 2**k < n + 1: k += 1 # k parity bits p = 0 for i in range(k): if (1 << i) & n: p ^= 1 s = 0 for i in range(k): p = 1 << i t = 0 for j in range(n): if (j + 1) & p: t ^= bitarray_in[j] if t: s += p if s: bitarray_in[s - 1] ^= 1 j = 0 bitarray_out = bitarray(n - k) for i in range(n): if (i + 1) & (i + 1) - 1 != 0: bitarray_out[j] = bitarray_in[i] j += 1 return bitarray_out if __name__ == "__main__": doctest.testmod() ``` -------------------------------- ### Add missing start and stop parameters to index() method Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst The index() method now includes the missing optional start and stop parameters. ```python add missing start and stop optional parameters to index() method ``` -------------------------------- ### Add optional start and stop arguments to .count() method Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst The .count() method now accepts optional start and stop arguments for more flexible counting within a specified range. ```python add optional start and stop arguments to ".count()" method ``` -------------------------------- ### Variable Length Encoding Example Breakdown Source: https://github.com/ilanschnell/bitarray/blob/master/doc/variable_length.rst Illustrates the step-by-step process of encoding a specific bitarray into the variable length format, showing how the raw bitarray is grouped, padded, and converted into hexadecimal bytes. ```text 01010110111001110 raw bitarray 0101 0110111 001110 grouped (4, 7, 7, ...) 0101 0110111 0011100 pad last group with zeros 0010101 0110111 0011100 add number of pad bits (1) to front (001) 10010101 10110111 00011100 add high bits (1, except 0 for last group) 0x95 0xb7 0x1c in hexadecimal - output stream ``` -------------------------------- ### Python Example: Double Precision Floating Point Number Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst This example illustrates how to represent and manipulate double-precision floating-point numbers using bitarrays. It is part of the bitarray library's examples. ```python from bitarray import bitarray from bitarray import util import struct # double precision floating point number def float2ba(f): return bitarray(bytes=struct.pack('>d', f)) def ba2float(b): return struct.unpack('>d', b.tobytes())[0] f = 3.14159 b = float2ba(f) print(b) print(ba2float(b)) ``` -------------------------------- ### Word Shift Example in C Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst Demonstrates word shifting operations in C, likely for low-level bit manipulation or performance optimization within the bitarray library's C implementation. ```c #include #include // Function to perform a right shift by 8 bits using word shifts // Assumes uint64_t for word operations uint64_t shift_r8(uint64_t value) { // This is a simplified example. Real implementation might involve // more complex logic for different shift amounts and endianness. // For a right shift by 8, we can conceptually think of shifting // the entire 64-bit word. However, bitarray's shift_r8 is more nuanced // as it deals with arbitrary bit positions, not just byte boundaries. // This C example is illustrative of the concept of word-level operations. // A direct C equivalent for bitarray's shift_r8 would be significantly // more complex, involving bit manipulation across word boundaries. // This example shows a basic uint64_t right shift. return value >> 8; } int main() { uint64_t data = 0x1122334455667788; // Example 64-bit data uint64_t shifted_data = shift_r8(data); printf("Original data: 0x%016llx\n", (unsigned long long)data); printf("Shifted data (by 8 bits): 0x%016llx\n", (unsigned long long)shifted_data); // Example demonstrating the concept of word shifts for bit manipulation // Imagine we want to shift bits within a larger structure. // The actual bitarray implementation uses these principles for efficiency. uint64_t val = 0xABCDEF0123456789; uint64_t shifted_val = (val >> 8) | (val << (64 - 8)); // Example of circular shift printf("Circular shift example: 0x%016llx\n", (unsigned long long)shifted_val); return 0; } ``` -------------------------------- ### Sieve of Eratosthenes using Bitarray Source: https://context7.com/ilanschnell/bitarray/llms.txt Implement the Sieve of Eratosthenes algorithm efficiently using bitarrays. This example demonstrates prime number generation, counting, and finding specific primes. ```python from bitarray import bitarray from bitarray.util import ones, count_n, sum_indices from math import isqrt def sieve(n): """Return bitarray where a[i] indicates if i is prime.""" a = ones(n) a[:2] = False # 0 and 1 are not prime for i in range(2, isqrt(n) + 1): if a[i]: # i is prime a[i*i::i] = False # Mark all multiples as not prime return a # Find all primes up to 100 primes = sieve(100) prime_list = list(primes.search(1)) print(prime_list) # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] # Count primes print(primes.count()) # 25 # Find the 10th prime print(count_n(primes, 10) - 1) # 29 # Count twin primes (primes that differ by 2) primes = sieve(1000) twin_count = primes.count(bitarray('101')) + 1 # +1 for (3,5) and (5,7) print(twin_count) # 35 # Sum of all primes below 1000 print(sum_indices(primes)) # 76127 ``` -------------------------------- ### LFSR Example Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst Illustrates the use of a Linear Feedback Shift Register (LFSR). This is typically used for generating pseudo-random numbers or for stream ciphers. ```python from bitarray import bitarray def lfsr(n, seed, poly): """ Generate a pseudo-random sequence using a Linear Feedback Shift Register. Args: n (int): The desired length of the sequence. seed (bitarray): The initial state of the LFSR. poly (list): The polynomial defining the feedback taps. Returns: bitarray: The generated pseudo-random sequence. """ state = seed.copy() result = bitarray() for _ in range(n): result.append(state[-1]) feedback = 0 for tap in poly: feedback ^= state[tap] state = bitarray([feedback]) + state[:-1] return result if __name__ == '__main__': # Example usage: # A 4-bit LFSR with seed 1101 and polynomial x^4 + x^1 + 1 (taps at indices 3, 0) seed = bitarray('1101') poly = [3, 0] # Corresponds to x^4 + x^1 + 1 sequence = lfsr(10, seed, poly) print(f"Generated sequence: {sequence}") # Another example: 8-bit LFSR seed_8bit = bitarray('10110010') poly_8bit = [7, 6, 3, 0] # Corresponds to x^8 + x^7 + x^6 + x^3 + 1 sequence_8bit = lfsr(20, seed_8bit, poly_8bit) print(f"Generated 8-bit sequence: {sequence_8bit}") ``` -------------------------------- ### Encode Bitarray to Variable Length Format Source: https://github.com/ilanschnell/bitarray/blob/master/doc/variable_length.rst Provides a direct Python example for encoding a bitarray into its variable length binary representation. This function is useful when you need to store or transmit bitarrays compactly. ```python >>> vl_encode(bitarray('01010110111001110')) b'\x95\xb7\x1c' ``` -------------------------------- ### Add exception to setup.py for README.rst Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst Includes an exception in setup.py to handle cases where README.rst cannot be opened. ```python add exception to setup.py when README.rst cannot be opened ``` -------------------------------- ### Create and Initialize Bitarrays Source: https://context7.com/ilanschnell/bitarray/llms.txt Demonstrates various ways to create bitarray objects, including from scratch, specified length, strings, iterables, bytes, and with explicit endianness. Imports are required. ```python from bitarray import bitarray # Create empty bitarray a = bitarray() # Create bitarray of specified length (all zeros) b = bitarray(10) # bitarray('0000000000') # Initialize from string (whitespace and underscores ignored) c = bitarray('1100 1010') # bitarray('11001010') d = bitarray('1111_0000') # bitarray('11110000') # Initialize from iterable e = bitarray([1, 0, 1, 1, 0]) # bitarray('10110') # Initialize from bytes (directly sets buffer) f = bitarray(b'\xff\x00') # bitarray('1111111100000000') # Specify bit-endianness g = bitarray('1100', endian='little') h = bitarray('1100', endian='big') # Import buffer from another object import array arr = array.array('B', [0xff, 0x00]) i = bitarray(buffer=arr, endian='big') # bitarray('1111111100000000') ``` -------------------------------- ### Basic bitarray Usage Source: https://github.com/ilanschnell/bitarray/blob/master/README.rst Demonstrates the creation and basic manipulation of bitarray objects, including appending, extending, initializing from strings and iterables, indexing, slicing, and counting elements. ```python from bitarray import bitarray a = bitarray() # create empty bitarray a.append(1) a.extend([1, 0]) a ``` ```python x = bitarray(2 ** 20) # bitarray of length 1048576 (initialized to 0) len(x) ``` ```python bitarray('1001 011') # initialize from string (whitespace is ignored) ``` ```python lst = [1, 0, False, True, True] a = bitarray(lst) # initialize from iterable a ``` ```python a[2] # indexing a single item will always return an integer ``` ```python a[2:4] # whereas indexing a slice will always return a bitarray ``` ```python a[2:3] # even when the slice length is just one ``` ```python a.count(1) ``` ```python a.remove(0) # removes first occurrence of 0 a ``` -------------------------------- ### Introduced .tobytes() and .frombytes() Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst Introduces .tobytes() and .frombytes() methods, marking .tostring() and .fromstring() as deprecated. ```python introduced ".tobytes()" and ".frombytes()" ( ".tostring()" and ".fromstring()" are now deprecated) ``` -------------------------------- ### Get Binary String Representation of Bitarray Source: https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst Use the `to01()` method to get the raw string of 0s and 1s for a bitarray. This is useful for interactive analysis but can be inefficient for large bitarrays. ```python >>> from bitarray import bitarray >>> a = bitarray('11001') >>> repr(a) # same as str(a) "bitarray('11001')" >>> a.to01() # the raw string of 0's and 1's '11001' ``` -------------------------------- ### Update documentation to use tobytes and frombytes Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst Documentation has been updated to use tobytes() and frombytes() methods, replacing the deprecated tostring() and fromstring(). ```python update documentation to use tobytes and frombytes, rather than tostring and fromstring (which are now deprecated) ``` -------------------------------- ### bitarray.bytereverse([start], [stop]) Source: https://github.com/ilanschnell/bitarray/blob/master/README.rst Reverses the bits within each byte of a specified range in the bitarray. ```APIDOC ## bitarray.bytereverse([start], [stop]) ### Description Reverses the bits within each byte in the specified byte range (`start` to `stop`). The reversal is done in-place and affects the buffer representation without changing the bit-endianness. Pad bits are unaffected. ### Method `bytereverse(start=0, stop=)` ### Endpoint N/A (This is a method) ### Parameters - **start** (int): The starting byte index (inclusive). Defaults to 0. - **stop** (int): The ending byte index (exclusive). Defaults to the end of the buffer. ### Request Example ```python from bitarray import bitarray a = bitarray('1100101000001111') # Represents two bytes: 11001010 and 00001111 a.bytereverse(0, 1) # Reverse bits in the first byte print(a) # Expected: bitarray('0101001100001111') a.bytereverse() # Reverse bits in all bytes print(a) # Expected: bitarray('1010001111110000') ``` ### Response Example Modifies the bitarray in-place. No return value. ``` -------------------------------- ### Using decodetree for efficient decoding Source: https://github.com/ilanschnell/bitarray/blob/master/README.rst Demonstrates how to create and use a decodetree object for faster decoding of bitarrays. ```APIDOC ## Using decodetree for efficient decoding ### Description This section illustrates the efficient use of the `decodetree` object for decoding bitarrays, which is faster than repeatedly passing prefix code dictionaries. ### Method `bitarray.decode(decodetree_object)` ### Endpoint N/A (This is a library usage example) ### Request Example ```python from bitarray import bitarray, decodetree t = decodetree({'a': bitarray('0'), 'b': bitarray('1')}) a = bitarray('0110') list(a.decode(t)) ``` ### Response Example ``` ['a', 'b', 'b', 'a'] ``` ``` -------------------------------- ### Python Bitarray Growth Pattern Visualization Source: https://github.com/ilanschnell/bitarray/blob/master/devel/resize/README.md Run this Python script to observe the bitarray's allocated size as it grows. It appends one bit at a time and prints the size whenever it changes. ```python from bitarray import bitarray b = bitarray() print(f"Initial size: {b.sizeof()}") for i in range(1000): b.append(0) new_size = b.sizeof() if new_size != b.sizeof(): print(f"Appended bit {i+1}, new size: {new_size}") ``` -------------------------------- ### bitarray.count(value, [start], [stop], [step]) Source: https://github.com/ilanschnell/bitarray/blob/master/README.rst Counts occurrences of a specific bit or sub-bitarray within a given range. ```APIDOC ## bitarray.count(value, [start], [stop], [step]) ### Description Counts the number of occurrences of `value` (a single bit or a sub-bitarray) within the specified slice `[start:stop:step]` of the bitarray. If `value` is a sub-bitarray, only non-overlapping occurrences are counted, and `step` must be 1. ### Method `count(value=1, start=0, stop=, step=1)` ### Endpoint N/A (This is a method) ### Parameters - **value** (int or bitarray): The bit (0 or 1) or sub-bitarray to count. - **start** (int): The starting index of the slice (inclusive). Defaults to 0. - **stop** (int): The ending index of the slice (exclusive). Defaults to the end of the bitarray. - **step** (int): The step of the slice. Defaults to 1. ### Request Example ```python from bitarray import bitarray a = bitarray('10110101') # Count occurrences of bit 1 a.count(1) # Returns 5 # Count occurrences of bit 0 in a slice a.count(0, 2, 7) # Counts 0s in '110101' -> 2 # Count occurrences of a sub-bitarray (non-overlapping) sub = bitarray('101') a.count(sub) # Counts '101' in '10110101' -> 2 ``` ### Response Example `int` - The number of occurrences. ``` -------------------------------- ### C Implementation of Bitarray Growth Pattern Source: https://github.com/ilanschnell/bitarray/blob/master/devel/resize/README.md This C program demonstrates the distilled `resize()` function, illustrating the bitarray's growth pattern. It produces the same output as the Python script. ```c #include #include // Simplified representation of bitarray growth // In a real implementation, this would involve memory allocation strategies. size_t get_new_capacity(size_t current_capacity) { if (current_capacity == 0) { return 8; // Initial capacity } // Example growth strategy: double the capacity, or a minimum increase size_t new_capacity = current_capacity * 2; if (new_capacity < current_capacity + 1) { new_capacity = current_capacity + 1; } return new_capacity; } int main() { size_t current_bits = 0; size_t current_capacity = 0; size_t previous_capacity = 0; printf("Initial capacity: %zu\n", current_capacity); for (int i = 0; i < 1000; ++i) { current_bits++; if (current_bits > current_capacity) { size_t old_capacity = current_capacity; current_capacity = get_new_capacity(current_capacity); if (current_capacity != previous_capacity) { printf("Appended bit %d, new capacity: %zu\n", i + 1, current_capacity); previous_capacity = current_capacity; } } } return 0; } ``` -------------------------------- ### Count and Search Methods Source: https://context7.com/ilanschnell/bitarray/llms.txt Demonstrates methods for counting occurrences of bits or sub-bitarrays, and finding their positions. Imports are required. ```python from bitarray import bitarray a = bitarray('10110101101') # Count occurrences print(a.count()) # 7 (count of 1s) print(a.count(0)) # 4 (count of 0s) print(a.count(1, 2, 8)) # 4 (count 1s in range [2:8]) # Count sub-bitarray (non-overlapping) a = bitarray('101010101') print(a.count(bitarray('101'))) # 2 # Find first occurrence (returns -1 if not found) a = bitarray('00101100') print(a.find(1)) # 2 print(a.find(1, 3)) # 4 (start from index 3) print(a.find(1, right=True)) # 5 (rightmost) print(a.find(bitarray('11')))# 4 # Index (like find but raises ValueError if not found) a = bitarray('00101100') print(a.index(1)) # 2 print(a.index(1, right=True))# 5 ``` -------------------------------- ### Use file context managers in tests Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst Tests have been updated to use file context managers for better resource management. ```python use file context managers in tests ``` -------------------------------- ### Bitarray Search Iterator Usage (v3+) Source: https://github.com/ilanschnell/bitarray/blob/master/doc/bitarray3.rst In bitarray version 3 and later, the .search() method returns an iterator. To get a list of search results, wrap the call in list(). ```python list(a.search()) ``` -------------------------------- ### Bitarray Decode Iterator Usage (v3+) Source: https://github.com/ilanschnell/bitarray/blob/master/doc/bitarray3.rst In bitarray version 3 and later, the .decode() method returns an iterator. To get a list of decoded items, wrap the call in list(). ```python list(a.decode()) ``` -------------------------------- ### Encode and Decode Bitarray Source: https://github.com/ilanschnell/bitarray/blob/master/doc/sparse_compression.rst Demonstrates encoding a sparse bitarray into a compact blob and then decoding it back. Ensure the bitarray and util functions are imported. ```python >>> from bitarray import bitarray >>> from bitarray.util import zeros, sc_encode, sc_decode >>> a = zeros(1 << 24, 'little') # 16 mbits >>> a[0xaa] = a[0xbbcc] = a[0xddeeff] = 1 >>> blob = sc_encode(a) >>> blob ``` ```python b'\x04\x00\x00\x00\x01\xc3\x03\xaa\x00\x00\xcc\xbb\x00\xff\xee\xdd\x00' ``` ```python >>> assert sc_decode(blob) == a ``` -------------------------------- ### Compression Performance Comparison Source: https://github.com/ilanschnell/bitarray/blob/master/doc/sparse_compression.rst Compares the compression and decompression speeds (in milliseconds) and compression ratios of different methods, including serialize, sc, gzip, and bz2, for a random bitarray. ```text compress (ms) decompress (ms) ratio ---------------------------------------------------------- serialize 3.876 1.002 1.0000 sc 5.502 2.703 0.0158 gzip 918.937 10.057 0.0169 bz2 59.500 32.611 0.0117 ``` -------------------------------- ### Frozenbitarrays: Immutable and Hashable Source: https://github.com/ilanschnell/bitarray/blob/master/README.rst Explains the concept of frozenbitarrays, highlighting their immutability and hashability, making them suitable for dictionary keys. ```APIDOC ## Frozenbitarrays: Immutable and Hashable ### Description A `frozenbitarray` is an immutable version of a `bitarray`. It can be used as a dictionary key because it is hashable. ### Method `frozenbitarray(initializer)` ### Endpoint N/A (This is a library usage example) ### Request Example ```python from bitarray import frozenbitarray key = frozenbitarray('1100011') {key: 'some value'} ``` ### Response Example ``` {frozenbitarray('1100011'): 'some value'} ``` ### Error Handling Example Attempting to modify a `frozenbitarray` will raise a `TypeError`. ```python key[3] = 1 ``` ### Error Response Example ``` TypeError: frozenbitarray is immutable ``` ``` -------------------------------- ### Add and Check Membership in Bitarray Source: https://context7.com/ilanschnell/bitarray/llms.txt Demonstrates adding items to a bitarray and checking for their membership. Note that membership checks may result in false positives. ```python # Add items for i in range(1000): bf.add(f"item_{i}") # Check membership print(f"item_500" in bf) # True (definitely added) print(f"item_999" in bf) # True (definitely added) print(f"item_9999" in bf) # False (probably, may have false positive) # Check false positive rate false_positives = sum(1 for i in range(1000, 2000) if f"item_{i}" in bf) print(f"False positive rate: {false_positives/1000:.2%}") ``` -------------------------------- ### Convert Bitarray to Byte Representation Source: https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst Use the `tobytes()` method to get the raw byte buffer representation of a bitarray. This is efficient for large bitarrays but not human-readable. Note that padding bits and endianness are not included in this buffer. ```python >>> a = bitarray('11001110000011010001110001111000010010101111000111100') >>> a.tobytes() # raw buffer b'\xce\r\x1cxJ\xf1\xe0' ``` -------------------------------- ### Sequence Operations and Indexing Source: https://context7.com/ilanschnell/bitarray/llms.txt Illustrates standard Python sequence operations like indexing, slicing, and assignment, including integer list and boolean mask indexing. Imports are required. ```python from bitarray import bitarray a = bitarray('11001010') # Single item access (returns int 0 or 1) print(a[0]) # 1 print(a[-1]) # 0 # Slice access (returns bitarray) print(a[2:5]) # bitarray('001') print(a[::2]) # bitarray('1010') # Slice assignment a[2:4] = bitarray('11') print(a) # bitarray('11111010') # Assign boolean to slice a[::2] = 0 # Set every other bit to 0 print(a) # bitarray('01010000') # Delete elements a = bitarray('11001010') del a[1:3] print(a) # bitarray('101010') # Integer list indexing (like NumPy) a = bitarray('11001010') print(a[[0, 2, 4]]) # bitarray('101') a[[1, 3, 5]] = 1 print(a) # bitarray('11111110') # Boolean mask indexing a = bitarray('11001010') mask = bitarray('10101010') print(a[mask]) # bitarray('1000') - elements where mask is 1 a[mask] = 0 # Set masked positions to 0 print(a) # bitarray('01000010') ``` -------------------------------- ### Find Multiples of 6 Not Sum of Two Middle Numbers Source: https://github.com/ilanschnell/bitarray/blob/master/examples/dubner.rst Identifies positive integers divisible by 6 (greater than 6) that are not the sum of two 'middle numbers'. This is achieved by searching for unset bits (0) in the `mark` bitarray, starting from index 2. ```python [6 * i for i in mark.search(0, 2)] ``` -------------------------------- ### Setting Bitarray Ranges Source: https://github.com/ilanschnell/bitarray/blob/master/README.rst Demonstrates setting all elements to 0 and then setting a specific range of elements to 1. This is equivalent to using the .setall() method. ```python >>> a = bitarray(30) >>> a[:] = 0 # set all elements to 0 - equivalent to a.setall(0) >>> a[10:25] = 1 # set elements in range(10, 25) to 1 >>> a bitarray('000000000011111111111111100000') ``` -------------------------------- ### Simplify how tests are run Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst The process for running tests has been simplified. ```python simplified how tests are run ``` -------------------------------- ### Pretty Print Utility Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst Introduces a utility function for pretty-printing bitarrays with formatting options. ```python bitarray.util.pprint() ``` -------------------------------- ### Optimize setrange() using memset() Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst The setrange() method has been optimized by utilizing the C-level memset() function for improved performance. ```python optimize "setrange()" (C-function) by using "memset()" ``` -------------------------------- ### Drop Python 2.4, 3.1 and 3.2 support Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst Support for Python 2.4, 3.1, and 3.2 has been removed. ```python drop Python 2.4, 3.1 and 3.2 support ``` -------------------------------- ### Bitwise Operations Source: https://context7.com/ilanschnell/bitarray/llms.txt Shows how to perform standard bitwise operations (NOT, AND, OR, XOR, shifts) and their in-place variants on bitarrays. Imports are required. ```python from bitarray import bitarray a = bitarray('11001010') b = bitarray('10101100') # Invert (NOT) print(~a) # bitarray('00110101') # AND print(a & b) # bitarray('10001000') # OR print(a | b) # bitarray('11101110') # XOR print(a ^ b) # bitarray('01100110') # Left shift (fills with zeros) print(a << 2) # bitarray('00101000') # Right shift (fills with zeros) print(a >> 2) # bitarray('00110010') # In-place operations c = bitarray('11110000') c &= bitarray('10101010') print(c) # bitarray('10100000') c |= bitarray('00001111') print(c) # bitarray('10101111') c ^= bitarray('11111111') print(c) # bitarray('01010000') c <<= 3 print(c) # bitarray('10000000') c >>= 5 print(c) # bitarray('00000100') ``` -------------------------------- ### Porting to Python 3.x Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst The library has been ported to be compatible with Python 3.x. ```python porting to Python 3.x (Roland Puntaier) ``` -------------------------------- ### Read and Write Bitarray to File Source: https://context7.com/ilanschnell/bitarray/llms.txt Use `tofile()` to write a bitarray to a file and `fromfile()` to read it back. `fromfile()` can optionally read a specific number of bytes. ```python from bitarray import bitarray a = bitarray('1100101011110000') with open('data.bin', 'wb') as f: a.tofile(f) ``` ```python from bitarray import bitarray a = bitarray() with open('data.bin', 'rb') as f: a.fromfile(f) print(a) # bitarray('1100101011110000') ``` ```python from bitarray import bitarray a = bitarray() with open('data.bin', 'rb') as f: a.fromfile(f, 1) # Read only 1 byte print(a) # bitarray('11001010') ``` -------------------------------- ### Explicit Little-Endian Initialization Source: https://github.com/ilanschnell/bitarray/blob/master/doc/endianness.rst Shows how to explicitly create a bitarray with little-endian representation. In little-endian, the least-significant bit comes first. ```python >>> a = bitarray(b'A', endian='little') >>> a bitarray('10000010') >>> a.endian 'little' ``` -------------------------------- ### Decode Sparse Bitarrays from Stream Source: https://context7.com/ilanschnell/bitarray/llms.txt Demonstrates decoding multiple sparse bitarrays from a single stream using sc_decode, where each call consumes one compressed bitarray. ```python from bitarray.util import sc_encode, sc_decode, zeros, ones from bitarray import bitarray # Decode from stream (consumes only one bitarray) a1 = zeros(100) a1[10] = a1[50] = 1 a2 = ones(100) a2[10] = a2[50] = 0 stream = iter(sc_encode(a1) + sc_encode(a2)) decoded1 = sc_decode(stream) decoded2 = sc_decode(stream) print(decoded1 == a1) # True print(decoded2 == a2) # True ``` -------------------------------- ### Base Conversion Utilities Source: https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst Provides utility functions for converting between bitarrays and base representations. ```python bitarray.util.ba2base() ``` ```python bitarray.util.base2ba() ``` -------------------------------- ### tolist() Source: https://github.com/ilanschnell/bitarray/blob/master/README.rst Converts the bitarray to a list of integers (0s and 1s). ```APIDOC ## tolist() ### Description Return bitarray as list of integers. ``a.tolist()`` equals ``list(a)``. Note that the list object being created will require 32 or 64 times more memory (depending on the machine architecture) than the bitarray object, which may cause a memory error if the bitarray is very large. ### Method tolist ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response - **list** - The bitarray as a list of integers. ``` -------------------------------- ### Generate Huffman Code Source: https://context7.com/ilanschnell/bitarray/llms.txt Create a Huffman code mapping from a frequency map using `huffman_code`. This is useful for data compression. ```python from bitarray import bitarray from bitarray.util import huffman_code freq = {'a': 45, 'b': 13, 'c': 12, 'd': 16, 'e': 9, 'f': 5} code = huffman_code(freq) print(code) # {'a': bitarray('0'), 'b': bitarray('101'), 'c': bitarray('100', # 'd': bitarray('111'), 'e': bitarray('1101'), 'f': bitarray('1100')} ``` -------------------------------- ### Compression Statistics by Probability Source: https://github.com/ilanschnell/bitarray/blob/master/doc/sparse_compression.rst Shows compression ratios and the distribution of block types (raw, type 1, type 2, type 3, type 4) for 256 mbit random bitarrays with varying probabilities of bits being 1. ```text p ratio raw type 1 type 2 type 3 type 4 ------------------------------------------------------------------------- 1.00000000 1.00024432 8192 0 0 0 0 0.50000000 1.00024432 8192 0 0 0 0 ``` -------------------------------- ### bitarray() constructor Source: https://github.com/ilanschnell/bitarray/blob/master/README.rst Details the constructor for the bitarray object, including various initializer types and optional arguments. ```APIDOC ## bitarray() constructor ### Description Initializes a new bitarray object. The initializer can be an integer, bytes, a string of '0's and '1's, or an iterable of bits. Optional arguments include `endian` and `buffer`. ### Method `bitarray(initializer=0, /, endian='big', buffer=None)` ### Endpoint N/A (This is a constructor) ### Parameters #### Initializer Types - **int**: Length of the bitarray, initialized to zeros. - **bytes** or **bytearray**: Initializes the buffer directly. - **str**: A string of '0's and '1's (whitespace and '_' are ignored). - **iterable**: An iterable of integers (0 or 1). #### Optional Keyword Arguments - **endian** (str): Bit-endianness. Allowed values: 'big' (default) or 'little'. Affects buffer representation. - **buffer** (object): An object exposing a buffer. Cannot be used with `initializer`. (New in version 2.3) ### Request Example ```python # Initialize with an integer (length) a = bitarray(10) # Creates a bitarray of 10 zeros # Initialize with bytes b = bitarray(b'\x01\x02') # Initialize with a string c = bitarray('101101') # Initialize with an iterable d = bitarray([0, 1, 0, 1]) # Initialize with endianness e = bitarray('101', endian='little') ``` ``` -------------------------------- ### fromfile() Source: https://github.com/ilanschnell/bitarray/blob/master/README.rst Extends the bitarray with bytes read from a file object. ```APIDOC ## fromfile(f, n=-1, /) ### Description Extend bitarray with up to ``n`` bytes read from file object ``f`` (or any other binary stream what supports a ``.read()`` method, e.g. ``io.BytesIO``). Each read byte will add eight bits to the bitarray. When ``n`` is omitted or negative, reads and extends all data until EOF. When ``n`` is non-negative but exceeds the available data, ``EOFError`` is raised. However, the available data is still read and extended. ### Method fromfile ### Parameters #### Path Parameters None #### Query Parameters - **f** (file object) - The file object to read from. - **n** (int) - The number of bytes to read. Defaults to -1 (read all). #### Request Body None ### Request Example None ### Response None ```