Using numpy.bitwise_or() function (3 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Overview

NumPy, a foundational package for numerical computing in Python, includes an assortment of functions for bitwise operations, one of which is numpy.bitwise_or(). This function is crucial for performing element-wise bitwise OR operations on array elements, enabling efficient manipulation and analysis of binary data.

The numpy.bitwise_or() function takes two arrays as input and returns a new array, with each element being the result of the bitwise OR operation. This operation is fundamental in various fields, including image processing, data analysis, and wherever binary arithmetic is applicable. In this tutorial, we’ll explore the functionality of numpy.bitwise_or() through three increasingly complex examples, demonstrating its versatility and power.

Basic Usage

Let’s start with the basics. The simplest form of usage is to perform a bitwise OR operation on two integers:

import numpy as np

a = 12 # Binary representation: 1100
b = 5  # Binary representation: 0101

c = np.bitwise_or(a, b)
print(c) # Output: 13, Binary representation: 1101

This example demonstrates a direct application of the function on scalar values, showcasing how numpy.bitwise_or() can transform these binary representations into a single, combined result.

Application on Arrays

Aside from scalars, numpy.bitwise_or() is commonly used with array inputs. Here’s how you can operate on two arrays:

import numpy as np

x = np.array([25, 24, 26, 23]) # Binary: 11001, 11000, 11010, 10111
y = np.array([31, 30, 29, 28]) # Binary: 11111, 11110, 11101, 11100

result = np.bitwise_or(x, y)
print(result) # Output: [31 30 31 31]

In this example, each element of the arrays x and y undergoes a bitwise OR operation. This is particularly useful in scenarios where bulk binary data needs to be manipulated efficiently.

Working with Multi-dimensional Arrays

NumPy’s bitwise_or() is not limited to one-dimensional arrays. It seamlessly works with multi-dimensional arrays, maintaining its element-wise operation principle. Consider the following example with two-dimensional arrays:

import numpy as np

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

result = np.bitwise_or(arr1, arr2)
print(result) # Output:
# [[ 5  6]
#  [ 7 12]]

This example highlights the function’s adaptability to handle complex array structures, further broadening its application scope.

Advanced Example: Image Processing

One exciting application of numpy.bitwise_or() is in the field of image processing. Images, which can be represented as arrays of pixel values, often need bitwise operations for various filters and effects. Here is a simple demonstration of applying a binary mask to an image:

import numpy as np
from skimage import data
from skimage.color import rgb2gray
import matplotlib.pyplot as plt

# Load an example image from scikit-image
image = data.camera()
# Convert the image to grayscale
image_gray = rgb2gray(image)

canvas = np.zeros_like(image_gray)
mask = canvas < 0.5
# Create a binary mask
canvas[mask] = 1

# Simulate a 'feature' in the image
feature = np.zeros_like(image_gray)
feature[200:300, 200:300] = 1

# Combine the feature with the canvas using bitwise_or
combined = np.bitwise_or(canvas, feature)

plt.imshow(combined, cmap='gray')
plt.show()

In this advanced example, we create a binary mask and a simulated feature, combining them using numpy.bitwise_or() to highlight specific areas of an image. This technique is foundational for more complex image processing tasks.

Conclusion

Through these examples, ranging from basic to advanced, we’ve explored the versatility and efficiency of numpy.bitwise_or() in handling binary data and its applications in arrays and image processing. Whether you’re manipulating simple binary values or intricate multi-dimensional arrays, numpy.bitwise_or() proves to be an invaluable tool in the arsenal of anyone working with numerical data in Python.