Open In App

Python – Bitwise AND of List

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among list elements. This is an essential utility as we come across bitwise operations many times. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using reduce() + lambda + “&” operator 
The above functions can be combined to perform this task. We can employ reduce() to accumulate the result of AND logic specified by the lambda function. Works only with Python2.
 

Python3




# Python code to demonstrate working of
# Bitwise AND of List
# Using reduce() + lambda + "&" operator
 
# initializing list
test_list = [4, 6, 2, 3, 8, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Bitwise AND of List
# Using reduce() + lambda + "&" operator
res = reduce(lambda x, y: x & y, test_list)
 
# printing result
print("The Bitwise AND of list elements are : " + str(res))


Output : 

The original list is : [4, 6, 2, 3, 8, 9]
The Bitwise AND of list elements are : 0

 

Time Complexity: O(n) where n is the number of elements in the string list. The reduce() + lambda + “&” operator is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) constant additional space is required

 
Method #2 : Using reduce() + operator.iand 
This task can also be performed using this method. In this the task performed by lambda function in above method is performed using iand function for cumulative AND operation. Works with Python2 only.
 

Python3




# Python code to demonstrate working of
# Bitwise AND of List
# Using functools.reduce() + operator.iand
from operator import iand
from functools import reduce
# initializing list
test_list = [4, 6, 2, 3, 8, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Bitwise AND of List
# Using functools.reduce() + operator.iand
res = reduce(iand, test_list)
 
# printing result
print("The Bitwise AND of list elements are : " + str(res))


Output : 

The original list is : [4, 6, 2, 3, 8, 9]
The Bitwise AND of list elements are : 0

 

Time complexity: O(n), where n is the length of the numbers list. The reduce() + operator.iand has a time complexity of O(n)
Auxiliary Space: O(1), constant space is required

Method #3: Using numpy.bitwise_and()

Note: Install numpy module using command “pip install numpy”

This method uses the numpy module’s bitwise_and() function to perform the bitwise AND operation on all elements of the list. This method works with both Python2 and Python3.

Python3




# Python code to demonstrate working of
# Bitwise AND of List
# Using numpy.bitwise_and()
 
import numpy as np
 
# initializing list
test_list = [4, 6, 2, 3, 8, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Bitwise AND of List
# Using numpy.bitwise_and()
res = np.bitwise_and.reduce(test_list)
 
# printing result
print("The Bitwise AND of list elements are : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output:

The original list is : [4, 6, 2, 3, 8, 9]
The Bitwise AND of list elements are : 0

Time complexity : O(n)
Auxiliary space: O(1)

Method #4 : Using a for loop and bitwise AND operator(&):

Python3




test_list = [4, 6, 2, 3, 8, 9]
print("The original list is : ", test_list)
res = test_list[0]
for i in range(1, len(test_list)):
    res = res & test_list[i]
print("The Bitwise AND of list elements are : ", res)
#This code is contributed by Jyothi pinjala


Output

The original list is :  [4, 6, 2, 3, 8, 9]
The Bitwise AND of list elements are :  0

Time complexity : O(n)
Auxiliary space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads