Exploring Bitarray in Python With List of Functions Available

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()      
  • 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


Declaring an empty bit array object

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:


This creates a bit array object with value 11011011

4)Declaring a bitarray object with list/tuple:

lst = [True, False, False, False, True, False]

This creates a bit array object with value 100010


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])
Miscellaneous Python Bitarray
Initializing bitarray with random python objects

Here, the empty values are interpreted as False and others as True

Functions available in Python Bitarray:

Sl NoFunctionDescriptionReturn type
1all()True when all bits in the array are Truebool
2any()True when any bit in the array is Truebool
3append(item, /)Append the truth value bool(item) to the end of the bitarray
4bytereverse()Reverses the bit order in place
5clear()Empties the bitarray
6copy()Copies the bitarraybitarray
7count(value=True, start=0, stop=<end of array>, /)Counts the frequency of a bool valueint
8extend(iterable or string, /)Extends the bitarray
9fill()Adds 0s to the end of bitarray to make it a multiple of 8int
10index(value, start=0, stop=<end of array>, /)Finds the index of the first occurrence of the given bool valueint
11insert(index, value, /)Inserts a bool value in the given index
12invert(index=<all bits>)Inverts all bits in place
13itersearch(bitarray, /)Searches for the given bitarrayiterartor
14length()Gives the length of the bitarrayint
15pop(index=-1, /)Deletes and returns the ith elementitem
16remove(value, /)Remove the first occurrence of given bool value
17reverse()Reverses the order of bits in place
18sort(reverse=False)Sorts the bits in place
Functions available for bitarray object

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


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!

5 1 vote
Article Rating
Notify of
Inline Feedbacks
View all comments