Numpy bitwise_and() Function



The NumPy bitwise_and() function performs a bitwise AND operation on corresponding elements of two arrays. This function compares the binary representations of the input elements by setting each bit in the result to 1 if both corresponding bits in the input arrays are 1 otherwise, the bit is set to 0.

This operation is element-wise which means processes each pair of elements from the input arrays independently. The bitwise_and() supports broadcasting, allowing it to handle arrays of different shapes by aligning them according to broadcasting rules.

This function is useful for binary data manipulation and low-level data processing. Below are the results of bitwise AND operation of bit combinations 1 and 0 −

  • Bitwise AND of 0 and 0: 0
  • Bitwise AND of 0 and 1: 0
  • Bitwise AND of 1 and 0: 0
  • Bitwise AND of 1 and 1: 1

Syntax

Following is the syntax of Numpy bitwise_and() function −

numpy.bitwise_or(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature])

Parameters

Following are the Parameters of Numpy bitwise_and() function −

  • x1: First input array or scalar.
  • x2: Second input array or scalar. Must be broadcastable to the shape of x1.
  • out(Optional): A location into which the result is stored. If provided then it must have a shape that matches the broadcasted output of x1 and x2.
  • where(Optional): A condition to determine where the operation is performed. The result is computed where this condition is True.
  • **kwargs: Additional keyword arguments.

Return value

This function returns the array with the result of bitwise AND operation.

Example 1

Following is the basic example of Numpy bitwise_and() function. Here this example shows how to compute the bitwise AND of two numbers −

import numpy as np 

# Print binary equivalents of 13 and 17
print('Binary equivalents of 13 and 17:')
a, b = 13, 17
print(bin(a), bin(b))
print('\n')  

# Print bitwise AND of 13 and 17
print('Bitwise AND of 13 and 17:')
print(np.bitwise_and(a, b))

Below is the output of bitwise_and() function applied on numbers 13 and 17 −

Binary equivalents of 13 and 17:
0b1101 0b10001

Bitwise AND of 13 and 17:
1

Example 2

Here in this example we show how to create two 2D NumPy arrays by displaying their properties and computing their element-wise bitwise AND−

import numpy as np

# Creating two numpy arrays using the array() method
# We have inserted elements of int type
arr1 = np.array([[49, 6, 61],
                 [82, 69, 29]])
arr2 = np.array([[40, 60, 61],
                 [81, 55, 32]])

# Display the arrays
print("Array 1:", arr1)
print("Array 2:", arr2)

# Get the type of the arrays
print("Our Array 1 type:", arr1.dtype)
print("Our Array 2 type:", arr2.dtype)

# Get the dimensions of the Arrays
print("Our Array 1 Dimensions:",arr1.ndim)
print("Our Array 2 Dimensions:",arr2.ndim)

# Get the shape of the Arrays
print("Our Array 1 Shape:",arr1.shape)
print("Our Array 2 Shape:",arr2.shape)

# To compute the bit-wise AND of two arrays element-wise, use the numpy.bitwise_and() method in Python Numpy
print("Result:",np.bitwise_and(arr1, arr2))

Following is the output for the above example −

Array 1: [[49  6 61]
 [82 69 29]]
Array 2: [[40 60 61]
 [81 55 32]]
Our Array 1 type: int64
Our Array 2 type: int64
Our Array 1 Dimensions: 2
Our Array 2 Dimensions: 2
Our Array 1 Shape: (2, 3)
Our Array 2 Shape: (2, 3)
Result: [[32  4 61]
 [80  5  0]]
numpy_binary_operators.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy