numpy.bitwise_and() – The NumPy bitwise_and Python Function

Introduction to NumPy bitwise_and

The bitwise AND of two array elements are compute using the numpy.bitwise_and() function. The bit-wise AND of the underlying binary representation of the numbers in the input arrays. Are compute by this function.

Syntax:

numpy.bitwise_and(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, ufunc ‘bitwise_and’)

Parameters:

  • arr1: [array_like] Input array.
  • arr2: [array_like] Input array.
  • out: [ndarray, optional] A location in which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provide or None, a freshly-allocate array is return.
  • **kwargs: allows you to pass keyword variable length of argument to a function. It is use when we want to handle named arguments in a function.
  • where: [array_like, optional] True value means to calculate the universal functions(ufunc) at that position. False value means to leave the value in the output alone.

Return: [ndarray or scalar] Result. This is a scalar if both x1 and x2 are scalars.

Code Examples of NumPy bitwise_and

Example 01: When inputs are numbers

# welcome to softhunt.net
# Python program explaining
# bitwise_and() function

import numpy as np
num1 = 5
num2 = 15

print ("Input number1 : ", num1)
print ("Input number2 : ", num2)
	
ans = np.bitwise_and(num1, num2)
print ("bitwise_and of 05 and 15 : ", ans)

Output:

Input number1 :  5
Input number2 :  15
bitwise_and of 05 and 15 :  5

Example 02: When inputs are arrays

# welcome to softhunt.net
# Python program explaining
# bitwise_and() function

import numpy as np

array1 = [3, 4,54]
array2 = [23, 2, 3]

print ("Input array1 : ", array1)
print ("Input array2 : ", array2)
	
ans = np.bitwise_and(array1, array2)
print ("Output array after bitwise_and: ", ans)

Output:

Input array1 :  [3, 4, 54]
Input array2 :  [23, 2, 3]
Output array after bitwise_and:  [3 0 2]

Example 03: When inputs are Boolean

# welcome to softhunt.net
# Python program explaining
# bitwise_and() function

import numpy as np

bool1 = [True, False, False, True, False, True]
bool2 = [False, True, False, True, True, False]

print ("Input array1 : ", bool1)
print ("Input array2 : ", bool2)
	
ans = np.bitwise_and(bool1, bool2)
print ("Output array after bitwise_and: ", ans)

Output:

Input array1 :  [True, False, False, True, False, True]
Input array2 :  [False, True, False, True, True, False]
Output array after bitwise_and:  [False False False  True False False]

FAQs

What is the cv2 bitwise_and in Python?

cv2.bitwise_and() is a function. That, as the name implies, does bitwise AND processing. The pixel value of the output picture is the AND of the values for each pixel of the input images src1 and src2.

What is meant by bitwise operation?

Working with individual bits, which are the smallest units of data in a computer, is call as bitwise operations. Each bit has a binary value of either 0 or 1. Despite the fact that computers can manipulate bits. They typically store data and execute instructions in bit multiples known as bytes. The majority of programming languages work with 8-bit, 16-bit, or 32-bit groups.

Characters that indicate operations to be done on single bits are known as bitwise operators. A bitwise operation compares the positions of individual bits in two-bit patterns of equal length.

Conclusion

That’s all for this article. If you have any confusion contact us through our website or email us at softhunt.n[email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. The NumPy rot90 Python Function

Leave a Comment