NumPy Binary operations: bitwise_or() function
numpy.bitwise_or() function
The bitwise_or() function is used to compute the bit-wise OR of two arrays element-wise.
Some common applications of numpy.bitwise_or() function include:
1. Image Processing: Manipulating individual bits in image data, such as setting or clearing specific bits in the color channels or alpha channel of an image.
2.
Cryptography: Combining or modifying binary keys or data during encryption or decryption processes.
3.
Error Detection and Correction: Implementing error detection and correction algorithms, such as Hamming codes, which involve bitwise operations on binary data.
4.
Digital Signal Processing: Performing bitwise operations on digital signals for various purposes, such as filtering or data compression.
Syntax:
numpy.bitwise_or(x1, x2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj ]) = <ufunc 'bitwise_or'>
Parameter:
Name | Description | Required / Optional |
---|---|---|
x1,x2 | Only integer and boolean types are handled. | Required |
out | 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. | Optional |
where | Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone. | Optional |
**kwargs | For other keyword-only arguments. |
Return value:
out: [ndarray or scalar]
Result: This is a scalar if both x1 and x2 are scalars.
Example: Bitwise OR operation with NumPy
>>> import numpy as np
>>> np.bitwise_or(34, 4)
38
>>> np.bitwise_or([34, 4], 3)
array([35, 7])
>>> np.bitwise_or([34, 4], [2, 3])
array([34, 7])
The above code demonstrates how to perform bitwise OR operations using the numpy.bitwise_or() function.
In the first example, the bitwise OR of two integers, 34 and 4, is calculated, resulting in 38.
The second example shows how to apply the bitwise OR operation between an array of integers and a single integer, where the integer is broadcasted to match the shape of the array, resulting in an array of [35, 7].
The third example performs the bitwise OR operation between two arrays of integers, returning an array of [34, 7].
Example: Bitwise OR operation with NumPy arrays and alternative syntax
>>> import numpy as np
>>> np.bitwise_or(np.array([2, 3, 256]), np.array([3, 3, 3]))
array([ 3, 3, 259])
>>> np.array([2, 3, 256]) | np.array([3, 3, 3])
array([ 3, 3, 259])
>>> np.bitwise_or([True, True], [False, True])
array([ True, True], dtype=bool)
The above code demonstrates how to perform bitwise OR operations on NumPy arrays using both the numpy.bitwise_or() function and the alternative "|" syntax.
In the first example, the bitwise OR operation is performed on two arrays with integer elements, resulting in an array of [3, 3, 259].
The second example shows the same operation, but using the "|" operator instead of the numpy.bitwise_or() function, yielding the same result.
The third example demonstrates the use of the numpy.bitwise_or() function with boolean values as input, resulting in a boolean array with the element-wise bitwise OR operation applied ([True, True]).
Python Code Editor:
Previous: bitwise_and()
Next: bitwise_xor()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/numpy/binary-operations/bitwise-or.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics