CuPy: NumPy & SciPy for GPU (CUDA 10.1 version)
—
Bitwise operations for integer and boolean arrays including AND, OR, XOR, NOT operations and bit shifting. These functions are essential for low-level data manipulation, boolean logic, and efficient computation with integer data types on GPU.
Element-wise bitwise logical operations on integer arrays.
def bitwise_and(x1, x2, out=None, **kwargs):
"""
Compute the bit-wise AND of two arrays element-wise.
Parameters:
- x1, x2: array_like, input arrays (integers or booleans)
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: result of bitwise AND operation
"""
def bitwise_or(x1, x2, out=None, **kwargs):
"""
Compute the bit-wise OR of two arrays element-wise.
Parameters:
- x1, x2: array_like, input arrays (integers or booleans)
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: result of bitwise OR operation
"""
def bitwise_xor(x1, x2, out=None, **kwargs):
"""
Compute the bit-wise XOR of two arrays element-wise.
Parameters:
- x1, x2: array_like, input arrays (integers or booleans)
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: result of bitwise XOR operation
"""
def bitwise_not(x, out=None, **kwargs):
"""
Compute bit-wise inversion, or bit-wise NOT, element-wise.
Parameters:
- x: array_like, input array (integers or booleans)
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: result of bitwise NOT operation
"""
def invert(x, out=None, **kwargs):
"""
Compute bit-wise inversion, or bit-wise NOT, element-wise.
Alias for bitwise_not.
Parameters:
- x: array_like, input array (integers or booleans)
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: result of bitwise NOT operation
"""Functions for shifting bits left or right in integer arrays.
def left_shift(x1, x2, out=None, **kwargs):
"""
Shift the bits of an integer to the left.
Parameters:
- x1: array_like of int, input array of integers
- x2: array_like of int, number of bits to shift left
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: result of left shift operation
"""
def right_shift(x1, x2, out=None, **kwargs):
"""
Shift the bits of an integer to the right.
Parameters:
- x1: array_like of int, input array of integers
- x2: array_like of int, number of bits to shift right
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: result of right shift operation
"""Functions for packing and unpacking binary representations.
def packbits(a, axis=None, bitorder='big'):
"""
Pack the elements of a binary-valued array into bits in a uint8 array.
Parameters:
- a: array_like, input array with values of 0 or 1
- axis: int, dimension over which bit-packing is done
- bitorder: str, bit order within bytes ('big' or 'little')
Returns:
cupy.ndarray: packed uint8 array
"""
def unpackbits(a, axis=None, count=None, bitorder='big'):
"""
Unpacks elements of a uint8 array into a binary-valued output array.
Parameters:
- a: cupy.ndarray of uint8, input array to unpack
- axis: int, dimension over which bit-unpacking is done
- count: int, number of elements to unpack along axis
- bitorder: str, bit order within bytes ('big' or 'little')
Returns:
cupy.ndarray: unpacked binary array
"""Utility functions for binary representation and manipulation.
def binary_repr(num, width=None):
"""
Return the binary representation of the input number as a string.
Parameters:
- num: int, input integer
- width: int, minimum number of digits in output
Returns:
str: binary representation string
"""import cupy as cp
# Create integer arrays
a = cp.array([1, 2, 3, 4, 5], dtype=cp.int32)
b = cp.array([5, 4, 3, 2, 1], dtype=cp.int32)
# Bitwise AND
result_and = cp.bitwise_and(a, b)
print(result_and) # [1 0 3 0 1]
# Bitwise OR
result_or = cp.bitwise_or(a, b)
print(result_or) # [5 6 3 6 5]
# Bitwise XOR
result_xor = cp.bitwise_xor(a, b)
print(result_xor) # [4 6 0 6 4]
# Bitwise NOT
result_not = cp.bitwise_not(a)
print(result_not) # [-2 -3 -4 -5 -6]import cupy as cp
# Create array of integers
values = cp.array([1, 2, 4, 8, 16], dtype=cp.int32)
# Left shift by 2 bits (multiply by 4)
left_shifted = cp.left_shift(values, 2)
print(left_shifted) # [4 8 16 32 64]
# Right shift by 1 bit (divide by 2)
right_shifted = cp.right_shift(values, 1)
print(right_shifted) # [0 1 2 4 8]import cupy as cp
# Create binary array
binary_data = cp.array([[1, 0, 1, 1, 0, 0, 1, 0],
[0, 1, 1, 0, 1, 0, 1, 1]], dtype=cp.uint8)
# Pack bits into bytes
packed = cp.packbits(binary_data, axis=1)
print(packed) # [[178] [107]]
# Unpack back to binary
unpacked = cp.unpackbits(packed, axis=1)
print(unpacked) # [[1 0 1 1 0 0 1 0] [0 1 1 0 1 0 1 1]]import cupy as cp
# Boolean arrays
bool_a = cp.array([True, False, True, False])
bool_b = cp.array([True, True, False, False])
# Bitwise operations work on boolean arrays too
and_result = cp.bitwise_and(bool_a, bool_b)
print(and_result) # [True False False False]
or_result = cp.bitwise_or(bool_a, bool_b)
print(or_result) # [True True True False]
xor_result = cp.bitwise_xor(bool_a, bool_b)
print(xor_result) # [False True True False]Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda101@9.6.1