In this article, we will learn about the python bit array. At first, let us understand what exactly a python bitarray is? A bitarray is an efficient way of representing Booleans in an array. Their behavior is similar to lists. They are also known as bit vectors, bit strings, or bit fields. In a contiguous block of memory, 8 bits represents 1 byte. This module is useful when one is dealing with compressed data using variable bit length encoding.
Key Features of Python Bitarray:
- The object of the bit array has a similar behavior as that of the list. So, the operations performed on the list like slicing can be performed on bit array object as well
- It is possible to pack or unpack bit array into other data formats like ndarray
- Buffer protocol is supported
- Encoding and decoding variable bit length prefix codes can be done at a faster rate
- Use of bitwise operators: &, |, ^, &=, |=, ^=, ~
- Bit array objects can be pickled and unpickled
How to use the Python Bitarray module?
We already know that the bit array objects behaves similar to a list. However, the distinction between a bitarray and list is:
- A bit array is homogeneous, whereas a list is heterogeneous
- Bit array can access the machine representation of an object
Declaring the Bitarray Object:
1) An empty python bitarray object
from bitarray import bitarray
arr = bitarray()
arr.append(False)
arr.append(True)
arr
- First, we imported the bit array module
- Then we declared an empty bit array object arr
- We then appended the Boolean value False and True to the object arr
- This is one way for declaring the bit array object
Output:
2) Declaring an empty bitarray object of specified size:
arr = bitarray(2**5)
This creates an empty bit array object of size 32
3) Declaring a bitarray using string:
bitarray('11011011')
This creates a bit array object with value 11011011
4)Declaring a bitarray object with list/tuple:
lst = [True, False, False, False, True, False]
bitarray(lst)
This creates a bit array object with value 100010
5)Miscellaneous:
Apart from all this, bit can be assigned from any python object given that the value can be interpreted as True or False.
arr = bitarray([41, '', True, {}, 'foo', None])
arr
Here, the empty values are interpreted as False and others as True
Functions available in Python Bitarray:
Sl No | Function | Description | Return type |
1 | all() | True when all bits in the array are True | bool |
2 | any() | True when any bit in the array is True | bool |
3 | append(item, /) | Append the truth value bool(item) to the end of the bitarray | – |
4 | bytereverse() | Reverses the bit order in place | – |
5 | clear() | Empties the bitarray | – |
6 | copy() | Copies the bitarray | bitarray |
7 | count(value=True, start=0, stop=<end of array>, /) | Counts the frequency of a bool value | int |
8 | extend(iterable or string, /) | Extends the bitarray | – |
9 | fill() | Adds 0s to the end of bitarray to make it a multiple of 8 | int |
10 | index(value, start=0, stop=<end of array>, /) | Finds the index of the first occurrence of the given bool value | int |
11 | insert(index, value, /) | Inserts a bool value in the given index | – |
12 | invert(index=<all bits>) | Inverts all bits in place | – |
13 | itersearch(bitarray, /) | Searches for the given bitarray | iterartor |
14 | length() | Gives the length of the bitarray | int |
15 | pop(index=-1, /) | Deletes and returns the ith element | item |
16 | remove(value, /) | Remove the first occurrence of given bool value | – |
17 | reverse() | Reverses the order of bits in place | – |
18 | sort(reverse=False) | Sorts the bits in place |
Advantages of Python Bitarray:
- It can easily be stored and manipulated in the register set
- Helps to exploit bit-level parallelism
- Makes maximum use of data cache
Disadvantages of Python Bitarray:
- If they are not compressed, they might become sparse
- It is expensive to access individual bits
Conclusion:
In this article, we learned about bitarray in python, its features, advantages, and disadvantages. Bitarray is primarily used when one wants to store bits, but they do not know in advance the number of bits that they want to store. There are many more functions available for a bitarray object, making using it easier and more efficient.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.
Happy Pythoning!