Binary Operations
×


Binary Operations

1483

Introduction to NumPy Binary Operations

NumPy, a powerful numerical computing library in Python, offers a suite of functions for performing binary operations on arrays. These operations are essential for tasks such as data masking, bitwise flags, and binary encoding. In this guide, we'll explore the various binary operations available in NumPy and how to utilize them effectively.

1. Bitwise AND: numpy.bitwise_and()

The bitwise_and() function computes the bit-wise AND of two arrays element-wise. It compares corresponding bits of two numbers and returns 1 if both bits are 1, otherwise 0.

import numpy as np
a = np.array([5, 10, 15], dtype=np.uint8)
b = np.array([3, 9, 18], dtype=np.uint8)
result = np.bitwise_and(a, b)
print(result)  # Output: [1 8 2]

2. Bitwise OR: numpy.bitwise_or()

The bitwise_or() function computes the bit-wise OR of two arrays element-wise. It compares corresponding bits of two numbers and returns 1 if at least one of the bits is 1.

import numpy as np
a = np.array([5, 10, 15], dtype=np.uint8)
b = np.array([3, 9, 18], dtype=np.uint8)
result = np.bitwise_or(a, b)
print(result)  # Output: [7 11 31]

3. Bitwise XOR: numpy.bitwise_xor()

The bitwise_xor() function computes the bit-wise XOR of two arrays element-wise. It compares corresponding bits of two numbers and returns 1 if the bits are different, otherwise 0.

import numpy as np
a = np.array([5, 10, 15], dtype=np.uint8)
b = np.array([3, 9, 18], dtype=np.uint8)
result = np.bitwise_xor(a, b)
print(result)  # Output: [6 3 29]

4. Bitwise NOT: numpy.bitwise_not()

The bitwise_not() function computes the bit-wise NOT of an array element-wise. It inverts all the bits of the input array.

import numpy as np
a = np.array([5, 10, 15], dtype=np.uint8)
result = np.bitwise_not(a)
print(result)  # Output: [250 245 240]

5. Left Shift: numpy.left_shift()

The left_shift() function shifts the bits of an integer to the left. This operation is equivalent to multiplying the number by 2 raised to the power of the number of bits shifted.

import numpy as np
a = np.array([5, 10, 15], dtype=np.uint8)
result = np.left_shift(a, 2)
print(result)  # Output: [20 40 60]

6. Right Shift: numpy.right_shift()

The right_shift() function shifts the bits of an integer to the right. This operation is equivalent to dividing the number by 2 raised to the power of the number of bits shifted.

import numpy as np
a = np.array([5, 10, 15], dtype=np.uint8)
result = np.right_shift(a, 2)
print(result)  # Output: [1 2 3]

7. Bit Packing: numpy.packbits()

The packbits() function packs the elements of a binary-valued array into bits in a uint8 array. This is useful for compressing binary data.

import numpy as np
a = np.array([True, False, True, False], dtype=bool)
result = np.packbits(a)
print(result)  # Output: [5]

8. Bit Unpacking: numpy.unpackbits()

The unpackbits() function unpacks elements of a uint8 array into a binary-valued output array. This is useful for expanding compressed binary data.

import numpy as np
a = np.array([5], dtype=np.uint8)
result = np.unpackbits(a)
print(result)  # Output: [0 0 0 0 1 0 1 0]

Conclusion

NumPy's binary operations provide efficient and convenient methods for performing bit-level manipulations on arrays. These operations are essential for tasks such as data masking, bitwise flags, and binary encoding. By mastering these functions, you can enhance your ability to work with binary data and create more efficient and versatile algorithms.



Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat