Python – Bitwise AND of List
Last Updated :
17 Apr, 2023
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
test_list = [ 4 , 6 , 2 , 3 , 8 , 9 ]
print ( "The original list is : " + str (test_list))
res = reduce ( lambda x, y: x & y, test_list)
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
from operator import iand
from functools import reduce
test_list = [ 4 , 6 , 2 , 3 , 8 , 9 ]
print ( "The original list is : " + str (test_list))
res = reduce (iand, test_list)
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
import numpy as np
test_list = [ 4 , 6 , 2 , 3 , 8 , 9 ]
print ( "The original list is : " + str (test_list))
res = np.bitwise_and. reduce (test_list)
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)
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)
|
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
Share your thoughts in the comments
Please Login to comment...