NumPy - bitwise_or() function
The Bitwise OR operator (|) is a binary operator which takes two bit patterns of equal length and performs the logical OR operation on each pair of corresponding bits. It returns 1 if either or both bits at the same position are 1, else returns 0.
Bit_1 | Bit_2 | Bit_1 | Bit_2 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 1 |
The example below describes how bitwise OR operator works:
50 | 25 returns 59 50 -> 110010 (In Binary) | 25 -> | 011001 (In Binary) ---- -------- 59 <- 111011 (In Binary)
The NumPy bitwise_or() function computes the bitwise OR of two arrays element-wise. This ufunc implements the C/Python operator |.
Syntax
numpy.bitwise_or(x1, x2, out=None)
Parameters
x1, x2 |
Required. Specify the arrays to be operated. Only integer and boolean types are handled. If x1.shape != x2.shape, they must be broadcastable to a common shape. |
out |
Optional. Specify a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. |
Return Value
Returns the result of bitwise OR operation. This is a scalar if both x1 and x2 are scalars.
Example:
In the example below, the bitwise_or() function is used to compute the bitwise OR of two scalars.
import numpy as np x = 50 y = 25 #Bitwise OR operation z = np.bitwise_or(x, y) #Displaying the result print("z =", z)
The output of the above code will be:
z = 59
Example:
The bitwise_or() function can be used with arrays where it computes the bitwise OR of two arrays element-wise.
import numpy as np x = np.array([[10, 20],[30, 40]]) y = np.array([[40, 30],[20, 10]]) #Bitwise OR operation z = np.bitwise_or(x, y) #Displaying the result print("z =") print(z)
The output of the above code will be:
z = [[42 30] [30 42]]
❮ NumPy - Binary Operators