NumPy Bitwise OR Practical

PythonPythonBeginner
Practice Now

Introduction

In this tutorial, you will learn about the bitwise_or() function of the NumPy library. This function is used to perform the bit-wise OR operation. We will cover its basic syntax, parameters, and provide multiple code examples.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Importing the Library

First, we need to import the NumPy library:

import numpy as np

Using the bitwise_or() Function with Two Scalar Values

Now, let's illustrate the use of the bitwise_or() function with two scalar values.

num1 = 15
num2 = 20

output = np.bitwise_or(num1, num2)

print("The bitwise OR of 15 and 20 is:", output)

Output:

The bitwise OR of 15 and 20 is: 31

In this example, we have used the bitwise_or() function to perform the OR operation on the two scalar values, num1 and num2.

Using the bitwise_or() Function with Two Arrays

Now, let's use the bitwise_or() function with two arrays:

ar1 = np.array([2, 8, 135])
ar2 = np.array([3, 5, 115])

output_arr = np.bitwise_or(ar1, ar2)

print("The output array after bitwise_or:", output_arr)

Output:

The output array after bitwise_or: [  3  13 247]

In this example, we have used the bitwise_or() function to perform the OR operation on the two arrays, ar1 and ar2, and the output is stored in the output_arr array.

Using the where Parameter

You can also use the where parameter to indicate a condition that is broadcast over the input:

x = np.array([1, 3, 5, 7])
y = np.array([8, 6, 4, 2])

output = np.bitwise_or(x, y, where=[True, False, True, False])

print("The output after bitwise_or operation:", output)

Output:

The output after bitwise_or operation: [ 8  3  5  2]

In this example, we have used the where parameter to perform the OR operation on specific input values based on the boolean condition specified.

Using the dtype Parameter

You can also use the dtype parameter to specify the data type of the output:

x = np.array([1, 3, 5, 7], dtype=np.int32)
y = np.array([8, 6, 4, 2], dtype=np.uint8)

output = np.bitwise_or(x, y, dtype=np.int64)

print("The output after bitwise_or operation:", output)

Output:

The output after bitwise_or operation: [ 8  7  5  7]

In this example, we have used the dtype parameter to specify the data type of the output array.

Summary

In this tutorial, we learned about the bitwise_or() function of the NumPy library. We explained its basic syntax and parameters, including x1, x2, out, where, casting, order, dtype, subok, signature, and extobj. We then provided multiple code examples to illustrate the use of the function.

Other Python Tutorials you may like