Python | Binary Operations in NumPy

In this tutorial, we will learn how to perform basic Binary Operations in Python using the NumPy library. You can learn about NumPy here- Python programs using NumPy

There are various binary operations such as–

  1. AND
  2. OR
  3. XOR

We will see them one by one.

AND Operation in NumPy

numpy.bitwise_and() – By this command, we perform AND operation on two numbers.

import numpy as np
num1 = 2
num2 = 3
 
print ("Number1 : ", num1)
print ("Number2 : ", num2) 
   
output = np.bitwise_and(num1, num2) 
print ("Bitwise AND operation of 2 and 3 is ", output) 

In this code, we are taking 2 numbers and performing AND operation on them using numpy.bitwise_and().

Output:

Number1 :  2
Number2 :  3
Bitwise AND operation of 2 and 3 :  2

OR Operation in NumPy

numpy.bitwise_or() – By this command, we perform OR operation on two numbers.

import numpy as np
num1 = 2
num2 = 3
 
print ("Number1 : ", num1)
print ("Number2 : ", num2) 
   
output = np.bitwise_or(num1, num2) 
print ("Bitwise OR operation of 2 and 3 os ", output) 

In this code, we are taking 2 numbers and performing OR operation on them using numpy.bitwise_or().

Output:

Number1 :  2
Number2 :  3
Bitwise OR operation of 2 and 3 :  3

XOR Operation

numpy.bitwise_xor() – By this command we perform XOR operation on two numbers.

import numpy as np
num1 = 2
num2 = 3
 
print ("Number1 : ", num1)
print ("Number2 : ", num2) 
   
output = np.bitwise_xor(num1, num2) 
print ("Bitwise XOR operation of 2 and 3 : ", output) 

In this code we are taking 2 numbers and performing XOR operation on them using numpy.bitwise_xor().

Output:

Number1 :  2
Number2 :  3
Bitwise XOR operation of 2 and 3 :  3

Left Shift of binary number in NumPy – Python

In this, we append the zeros to the right of the binary number. It is the same as multiplying our number in decimal form by 2**(shift).

import numpy as np
num = 3
shift = 3
 
print ("Number : ", num)
print ("Shift you needed : ", shift ) 
   
output = np.left_shift(num, shift) 
print ("Result(After shifting) : ", output)

Output:

Number :  3
Shift you needed :  3
Result(After shifting)  :  24

In this code we used left_shift() to shift the binary number by shift bits.

Right Shift of binary number

In this we append the zeros to the left of the binary number. It is the same as multiplying our number in decimal form by 2**(shift).

import numpy as np
num = 3
shift = 2
print ("Number : ", num)
print ("Shift you needed : ", shift ) 
output = np.right_shift(num, shift) 
print ("Result(After shifting) : ", output)

Output:

Number :  12
Shift you needed :  2
Result(After shifting) :  3

In this code, we used right_shift() to shift the binary number by shift bits.

You can perform all these functions on an array also. For AND, OR, and XOR operations arrays must be of the same size, and the operation is performed on an element with the same indexes in both arrays.

I hope you enjoyed the article.

Leave a Reply

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