Numpy bitwise_or() Function



The Numpy bitwise_or() function performs element-wise bitwise OR operation between two arrays or between an array and a scalar. Each bit of the output is set to 1 if at least one of the corresponding bits of the input operands is 1.

The arrays must have the same shape or be broadcastable to a common shape. This function is commonly used in scenarios requiring bit-level manipulation such as image processing or binary masking.

The result is a new array containing the bitwise OR of the input array's elements with the same shape and dtype as the input arrays. Below are the results of bitwise OR operation of bit combinations 1 and 0 −

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

Syntax

Following is the syntax of the Numpy bitwise_or() function −

numpy.bitwise_or(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature])
  • x1, x2 (like an array): These arrays used for input to compute the element-wise bitwise OR for these arrays.
  • out(Optional): This is the directory where the outcome is kept. It needs to have a shape that the inputs broadcast to, if one is given. A newly-allocated array is returned if it is not supplied.
  • where(Optional): Broadcast over the input is this condition. The ufunc result will be set to the out array at locations where the condition is True. If not, the initial value of the out array will be maintained.
  • casting(Optional): This parameter determines the type of data casting that is permitted. 'same_kind' is the default value.
  • order(Optional): Regulates the result's memory layout order. Returns 'K' by default.
  • dtype(Optional): This parameter overrides the dtype of the result.
  • subok(Optional): If True, sub-classes will be passed through. If False, the result will be a base-class array.
  • signature(Optional): This parameter is used for more advanced ufunc signature support.

Return value

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

Example 1

Following is the basic example of Numpy bitwise_or() function in which shows the bitwise OR operation of two integer values −

import numpy as np

a, b = 13, 17
print('Binary equivalents of 13 and 17:')
print(bin(a), bin(b))

print('Bitwise OR of 13 and 17:')
print(np.bitwise_or(a, b))

Its output is as follows −

Binary equivalents of 13 and 17:
0b1101 0b10001
Bitwise OR of 13 and 17:
29

Example 2

Here in this example we are using the bitwise_or() function to perform the bitwise OR operation element-wise between the two arrays −

import numpy as np

# Define two NumPy arrays
arr1 = np.array([10, 20, 30])
arr2 = np.array([1, 2, 3])

# Perform the bitwise OR operation
result = np.bitwise_or(arr1, arr2)

# Print the result
print("Array 1:", arr1)
print("Array 2:", arr2)
print("Bitwise OR result:", result)

Below is the output of bitwise OR operation performed on two arrays −

Array 1: [10 20 30]
Array 2: [1 2 3]
Bitwise OR result: [11 22 31]

Example 3

Broadcasting in NumPy allows for the element-wise operations on arrays of different shapes by aligning them to a common shape. Here in this example we show how the bitwise OR operation work with broadcasting −

import numpy as np

# Define the arrays
a = np.array([1, 2, 4])        # Shape: (3,)
b = np.array([[5, 6, 7]])      # Shape: (1, 3)

# Perform bitwise OR with broadcasting
result = np.bitwise_or(a, b)

print("Array a:", a)
print("Array b:", b)
print("Bitwise OR result:\n", result)

Following is the Output of the above example −

Array a: [1 2 4]
Array b: [[5 6 7]]
Bitwise OR result:
 [[5 6 7]]
numpy_binary_operators.htm
Advertisements