# Harnessing Bitpacking Power: Efficient Storage of Integer Ranges in Python

Bitpacking is an efficient technique used to compress data, particularly when dealing with large collections of numbers. This article will focus on the application of bitpacking in Python, demonstrating its effectiveness in storing integers of varying ranges.

## Understanding Bitpacking

Bitpacking is a form of data compression that involves the efficient packing of values into bit arrays. The primary advantage of bitpacking is the significant reduction in memory usage, especially when storing large amounts of data. However, the technique requires a nuanced understanding of binary number representation and bitwise operations.

The bitpacking process involves representing each number in a sequence with the smallest number of bits possible, and then merging these representations into a single, contiguous block of memory. In contrast, the conventional storage of integers typically uses a fixed amount of memory for each number, regardless of its actual size.

## Bitpacking in Python: A Practical Approach

Python, with its rich collection of built-in functions and modules, provides a straightforward way to implement bitpacking. The `int.to_bytes()`

and `int.from_bytes()`

methods, along with bitwise shift operators (`<<`

and `>>`

), are especially useful in this context.

Firstly, it's crucial to determine the number of bits required to represent the largest number in our data set. The built-in `bit_length()`

method, provided by Python's `int`

type, comes in handy for this task. It returns the number of bits necessary to represent an integer in binary, excluding the sign and leading zeros.

To illustrate, let's consider a sequence of 512 random integers, which can range from negative to positive values:

```
import random
random.seed(0)
integers = [random.randint(-1000, 1000) for _ in range(512)]
```

To find the required bit size, we calculate the bit length of the maximum absolute value in our list:

```
bit_size = max(abs(int(i)) for i in integers).bit_length()
```

We now have the necessary bit size to represent the largest number in our data sequence. We can proceed to pack our integers.

## Packing and Unpacking Functions

We define two functions: `bitpack_integers`

for packing the integers, and `unpack_integers`

for retrieving the original integers from the packed data:

```
def bitpack_integers(integers, bit_size):
byte_size = (bit_size + 7) // 8 # Number of bytes needed for each integer
packed = bytearray()
for i in integers:
i_bytes = int(i).to_bytes(byte_size, 'little', signed=True)
packed.extend(i_bytes)
return packed
def unpack_integers(packed, bit_size):
byte_size = (bit_size + 7) // 8 # Number of bytes used for each integer
integers = []
for i in range(0, len(packed), byte_size):
i_bytes = packed[i:i+byte_size]
integers.append(int.from_bytes(i_bytes, 'little', signed=True))
return integers
```

These functions use Python's `to_bytes`

and `from_bytes`

methods for conversion between integers and bytes. The 'little' argument specifies that the bytes are in little-endian order, which is the byte order used by most modern computers.

## Validating the Results

After packing and unpacking the integers, we can verify that the unpacked integers match the original integers exactly:

```
# Bitpack the integers
packed = bitpack_integers(integers, bit_size)
# Unpack the integers
unpacked = unpack_integers(packed, bit_size)
# Verify that the unpacked integers are the same as the original integers
validity = integers == unpacked
print(validity) # Should print: True
```

This will return `True`

, confirming the accuracy of our bitpacking and unpacking functions.

## Testing our bitpacking implementation

Our test code will perform the following steps to verify the bitpacking implementation:

- Generate a sequence of
**512**random integers, each in the range from**-1000**to**1000**. - Calculate the minimum bit size needed to represent the largest absolute integer in the sequence without loss.
- Bitpack the sequence of integers into the smallest possible number of bits using the calculated bit size. The bitpacking function will convert each integer to bytes and pack these bytes into a byte array.
- Unpack the bitpacked data back into a sequence of integers. The unpacking function will extract the bytes for each integer from the byte array and convert these bytes back into integers.
- Verify the validity of the bitpacking and unpacking process by checking that the unpacked sequence of integers matches the original sequence exactly.
- Calculate and print the compression ratio, which is the ratio of the original size of the data (in bytes) to the packed size. This provides a measure of the efficiency of the bitpacking process.

```
# Generate a sequence of 512 random integers between -1000 and 1000
random.seed(0)
integers = [random.randint(-1000, 1000) for _ in range(512)]
# Function to measure the size of an object in bytes
def sizeof(obj):
return len(obj) if isinstance(obj, bytearray) else len(bytearray(obj))
# Calculate the bit size needed for these integers
bit_size = max(abs(int(i)) for i in integers).bit_length()
# Bitpack the integers
packed = bitpack_integers(integers, bit_size)
# Unpack the integers
unpacked = unpack_integers(packed, bit_size)
# Verify that the unpacked integers are the same as the original integers
validity = integers == unpacked
# Calculate the original and packed sizes in bytes
original_size = len(integers) * 4 # Each integer uses 4 bytes
packed_size = len(packed) # Each byte in the packed data uses 1 byte
# Calculate the compression ratio
compression_ratio = original_size / packed_size
# Print the validity and compression ratio
validity, compression_ratio
```

If everything works as it should, we should see something like `(True, 2.0)`

.

The compression ratio is computed as the size of the original data divided by the size of the compressed (bitpacked) data. In this case, the compression ratio is 2, which means that the size of the original data is twice the size of the compressed data.

Here's why the compression ratio is 2 in this specific scenario:

**Original Data**: The original data consists of 512 integers. Each integer in Python typically requires 4 bytes (32 bits) of storage (assuming a 32-bit Python build), regardless of the actual size of the integer. So, the total size of the original data is \(512 \times 4\) bytes or 2048 bytes.

**Compressed Data**: The compressed data is created by bitpacking the integers. In this scenario, the bitpacking function determined that each integer could be represented using 10 bits (because the largest absolute value among the integers required 10 bits). However, because bytes are the smallest addressable unit of data in most computer systems, each packed integer actually uses up 2 bytes (16 bits), which is the smallest number of bytes that can accommodate 10 bits. Therefore, the total size of the compressed data is \(512 \times 2\) bytes or 1024 bytes.

Dividing the size of the original data (2048 bytes) by the size of the compressed data (1024 bytes) gives us a compression ratio of 2. This indicates that the original data is twice as large as the compressed data, showing that the bitpacking process has effectively halved the data size.

## Conclusion

In summary, bitpacking offers an effective method for compressing large collections of integers, reducing memory usage while maintaining the integrity of the data. By utilizing Python's built-in functions and understanding of binary operations, we can implement bitpacking with relative ease. This technique is especially beneficial in applications involving large datasets, where efficient storage and retrieval of data are paramount.