numpy.bitwise_or() – The NumPy bitwise_or Python Function

Introduction to NumPy bitwise_or

The bitwise OR of two array elements are computed using the numpy.bitwise_or() function. The bit-wise OR of the underlying binary representation of the numbers in the input arrays are computed by this function.

Syntax:

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

Parameters:

  • arr1: [array_like] Input array.
  • arr2: [array_like] Input array.
  • out: [ndarray, optional] A location into which the result is stored.
    • If provided, it must have a shape that the inputs broadcast to.
    • If not provides or None, a freshly-allocated array is return.
  • **kwargs: allows you to pass keyword variable length of argument to a function. It is use when we want to handle a name argument 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_or

Example 01: When inputs are numbers

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

import numpy as np
num1 = 5
num2 = 15

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

Output:

Input number1 :  5
Input number2 :  15
bitwise_or of 5 and 15 :  15

Example 02: When inputs are arrays

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

import numpy as np

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

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

Output:

Input array1 :  [3, 4, 54]
Input array2 :  [23, 2, 3]
Output array after bitwise_or:  [23  6 55]

Example 03: When inputs are Boolean

# welcome to softhunt.net
# Python program explaining
# bitwise_or() 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)
	
np = np.bitwise_or(bool1, bool2)
print ("Output array after bitwise_or: ", np)

Output:

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

FAQs

What is Arr Numpy?

Arr is the short form of an array – A NumPy array is a grid of identical-type items index by a tuple of nonnegative integers. The array’s rank is the number of dimensions; the shape is a tuple of numbers indicating the array’s size along each dimension.

How does NumPy store data?

Numpy arrays are store in a single block of memory that is contiguous (continuous). Memory divides into two categories: dimensions and strides. When traversing an array, strides are the number of bytes you must step in each dimension.

Conclusion

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

Suggested Articles:

  1. numpy.bitwise_and() – The NumPy bitwise_and Python Function

Leave a Comment