NumPy Bitwise Operators with Examples

Bitwise operations are fundamental operations performed at the bit level of binary representations of numbers. In computer programming, these operations are used to manipulate individual bits, enabling a wide range of tasks like masking, setting, toggling, and more. In this tutorial, we will learn about the various bitwise operators in NumPy you can use in your code to make your program more efficient!

Understanding Bitwise Operations in NumPy

NumPy Bitwise operations are applied to each individual bit of binary number. Here’s a brief overview of the common bitwise operators:

Bitwise AND (&): The bitwise AND (&) operator combines the appropriate bits of two integers logically. The result is 1 if both bits are 1, 0 otherwise.

Bitwise OR (|): The bitwise OR operator (|) logically ORs the corresponding bits of two integers. The result is 1 if either bit is 1, 0 if neither bit is 1, etc.

Bitwise XOR (^): Using two integers’ corresponding bits, this operator performs a logical exclusive OR operation. The outcome is either 1 or 0, depending on whether the bits are different.

Bitwise NOT (~): This operator flips an integer’s bits, converting 0s to 1s and 1s to 0s.

Left Shift (<<): The left shift () operator moves an integer’s bits to the left by the provided number of places. This is equivalent to raising the number by 2 to the shift count power.

Right Shift (>>): This operator moves an integer’s bits to the right by a predetermined amount. As a result, the number is divided by 2, elevated to the shift count.

Bitwise Invert (~): This operator effectively flips 0s into 1s and 1s into 0s by inverting all the bits of an integer.

NumPy Bitwise Operators-

Bitwise AND (&)

The numpy.bitwise_and() function performs an element-wise bitwise AND operation on two input arrays.

Variable-1 Variable-2 Output
0 0 0
0 1 0
1 0 0
1 1 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_and(a, b)
print(result)

Output:

[1 8 2]

Bitwise OR (|)

The numpy.bitwise_or() function performs an element-wise bitwise OR operation on two input arrays.

Variable-1 Variable-2 Output
0 0 0
0 1 1
1 0 1
1 1 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]

Bitwise XOR (^)

The numpy.bitwise_xor() function performs an element-wise bitwise XOR operation on two input arrays.

Variable-1 Variable-2 Output
0 0 0
0 1 1
1 0 1
1 1 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]

Bitwise NOT (~)

The numpy.bitwise_not() function performs an element-wise bitwise NOT operation on an input array.

Variable-1 Output
0 1
1 0

import numpy as np

a = np.array([5, 10, 15], dtype=np.uint8)

result = np.bitwise_not(a)
print(result)

Output:

[250 245 240]

Left Shift (<<) and Right Shift (>>)

The numpy.left_shift() and numpy.right_shift() functions perform element-wise left and right shifts on an input array.

import numpy as np

a = np.array([5, 10, 15], dtype=np.uint8)

left_shifted = np.left_shift(a, 2)
right_shifted = np.right_shift(a, 1)

print(left_shifted)
print(right_shifted)

Output:

[20 40 60]
[ 2 5 7]

Bitwise Invert (~)

The numpy.bitwise_not() function can also be used to perform an element-wise bitwise inversion on an input array, effectively flipping all the bits.

import numpy as np

a = np.array([5, 10, 15], dtype=np.uint8)

inverted_result = np.bitwise_not(a)
print(inverted_result)

Output:

[250 245 240]

Conclusion

Congratulations! You have learned about various bitwise operators in NumPy. These operators play a crucial role in tasks such as data masking, bitwise flags, and binary encoding. By mastering these operators, you can enhance your ability to work with binary data and create more efficient and versatile algorithms. Happy coding!