NumPy Bitwise Operators with Examples

Free NumPy course with real-time projects Start Now!!

NumPy includes a package to perform bitwise operations on the array elements. These NumPy bitwise operators perform bit by bit operations. It performs the function of two-bit values to produce a new value.

There are functions to convert the elements into their binary representation and then apply operations on the bits.

NumPy Bitwise Operators

This is a specific package that applies bitwise operations on the binary format of elements. These functions compare the binary value of elements and then produce output. There are 6 basic bitwise operations available in NumPy

1. bitwise_and()- It calculates the bit-wise AND operation between two array elements.
2. bitwise_or()- It calculates the bit-wise OR operation between two array elements.
3. invert()- It calculates the bit-wise NOT operation between two array elements.
4. bitwise_xor()- It calculates the bit-wise OR operation between two array elements.
5. left_shift()- This operator shifts the bits of the binary representation of the element towards left.
6. right_shift()- This operator shifts the bits of the binary representation of the element towards the right.

1. Bitwise AND operator

The function performs bitwise AND on two array elements. The bitwise function performs an operation on the corresponding bits of the binary representation of the operands i.e. elements. The output of the operation depends on the AND truth table.

If both the corresponding values are 1 only then the output will be 1, otherwise 0. Here 1 can also is equivalent to True and 0 as False. Hence the result will be True only if both the values are True, otherwise, it will result to be False.

Truth Table for AND

abAND(a,b)
000
010
100
111
import numpy as np
a=15
b=35
print(bin(a))
print(bin(b))
np.bitwise_and(a,b)

Output

0b1111
0b100011
3

2. Bitwise OR operator

The function performs bitwise OR on the binary representation of array elements. The output of the operation depends on the OR truth table. If one or both the corresponding values are 1 then the output will be 1, otherwise 0.

Here 1 is given as True and 0 as False. Hence the result will be True if one or both the values are True, otherwise, it will result to be False.

Truth Table for AND

abOR(a, b)
000
011
101
111
import numpy as np
a=15
b=35
print(bin(a))
print(bin(b))
np.bitwise_or(a,b)

Output

0b1111
0b100011
47

3. NumPy Invert Operator

This operator is the same as the NOT operator. It calculates the bitwise NOT of the binary representation of the elements. In the case of a signed integer, this function results as its 2’s complement. The output depends on the NOT truth table. It inverts the input value in the output.

aOUTPUT
01
10
import numpy as np
a=15
print(bin(a))
np.invert(a)

Output

0b1111
-16

4. NumPy Bitwise XOR operator

The function performs bitwise XOR on two array elements. We perform the bitwise operation on the corresponding bits of the binary representation of the operands i.e. elements.

The output of the operation depends on the XOR truth table. If both the corresponding values are the same then the output will be 0, otherwise 1.

abXOR(a, b)
000
011
101
110
import numpy as np
a=15
b=35
print(bin(a))
print(bin(b))
np.bitwise_xor(a,b)

Output

0b1111
0b100011
44

5. NumPy Left Shift Operator

The NumPy left shift operator shifts the binary representation of array elements towards the left. The function takes two parameters – array and the number of positions to shift. The array shifts towards left by appending zeros to its right.

import numpy as np
num = 15
shift = 3
np.left_shift(num,shift) 

Output

120

6. NumPy Right Shift Operator

The NumPy right shift operator shifts the binary representation of array elements towards the right. The function takes two parameters – array and the number of positions to shift.

import numpy as np
num = 15
shift = 3
np.right_shift(num,shift) 

Output

1

Summary

The binary operator performs bitwise operations on the array’s elements. It is a very useful package in NumPy. It has the ease of functionality as there functions even for conversion to binary format. The logics are fed in the functions that produce the correct results.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *