The bitwise_or() function of NumPy module

The numpy function bitwise_or() works on two numpy array-like instances. The integer elements of each member is done a bitwise OR and the result is returned as an ndarray.

Bitwise Operations Updated 24 Sep 2024 1 min read

Overview

  • Bitwise OR operation compares the bit pattern of two operands. A pair of bits are taken from each operand and the OR operation is applied. If both the bits are zero than the resultant bit is zero. If any of the bits is one than the resultant bit is one.
  • The bitwise_or() function of NumPy module applies bitwise OR between the integer elements of two array-like instances and returns the results as an ndarray.

Example

python
# Example Python program that does a bitwise

            # OR operation on the elements of two numpy

            # ndarray instances and returns the resultant ndarray

            import numpy

# For a given number return the binary number

            toBinary = lambda y: numpy.binary_repr(y, width = 6)

            txBase = numpy.vectorize(toBinary)

# Number and Masks

            numberArray = numpy.ndarray(shape = (1,3), dtype = numpy.int32)

            numberArray[0][0] = 20

            numberArray[0][1] = 25

            numberArray[0][2] = 30

maskArray  = numpy.ndarray(shape = (1,3), dtype = numpy.int32)

            maskArray[0][0] = 3

            maskArray[0][1] = 5

            maskArray[0][2] = 6

# Do bitwise OR

            bitORResult = numpy.bitwise_or(numberArray, maskArray)

# Print results

            print(txBase(numberArray))

            print(txBase(maskArray))

            print(txBase(bitORResult))

Output

Output
['010100' '011001' '011110']

['000011' '000101' '000110']

['010111' '011101' '011110']