BITWISE METHODS IN NUMPY
In this tutorial, we are going to learn about the bitwise methods in numpy which are AND, OR, INVERT and many more along with the handy examples for more clarity.
Wha are Bitwise Methods?
Bitwise methods in numpy perform operations on bits. The main reason for using bitwise method is to combine values to form a new value.
numpy.bitwise_and()
This method is used for computing bit-wise AND of elements from two arrays and returns the binary representation of the AND operation performed. If one of the operands is set to 1 and the other one is set to 0 then the answer bit in the AND operation will be set to 0.Remember that the input parameter should be of integer. Let’s look at an example to know more about numpy bitwise_and operator:
import numpy as np
x = 20
y = 30
print("binary display of x:",bin(x))
print("binary display of y:",bin(y))
print("Bitwise-and of x and y: ",np.bitwise_and(x,y))
numpy.bitwise_or()
This method is used for computing bit-wise OR of elements from two arrays and returns the binary representation of the OR operation performed. If one of the operands is set to 1 and the other one is set to 0 then the answer bit in the OR operation will be set to 1. Remember that the input parameter should be of integer. Let’s look at an example to know more about numpy bitwise_or operator:
import numpy as np
x = 20
y = 30
print("binary display of x:",bin(x))
print("binary display of y:",bin(y))
print("Bitwise-or of x and y: ",np.bitwise_or(x,y))
numpy.invert()
This function is used for calculating bitwise inversion of the array of elements. It is similar to the AND bitwise operation in numpy if integers are represented in the input arrays. Let’s look at an example to know more about numpy invert operator:
Example 1:
#Simple Example import numpy as np input_num = 10 print ("Entered number : ", input_num) output_num = np.invert(input_num) print ("inversion of 10 : ", output_num)
Output:
Entered number : 10 inversion of 10 : -11
Example 2:
#Array Example import numpy as np input_array = [2, 0, 25] print ("Entered array : ", input_array) output_array = np.invert(input_array) print ("Output array after inversion: ", output_array)
Output:
Entered array : [2, 0, 25] Output array after inversion: [ -3 -1 -26]
Example 3:
#Boolean example import numpy as np input_boolean = [True, False] print("Entered boolean array: ", input_boolean) output_boolean = np.invert(input_boolean) print("Output Boolean: ", output_boolean)
Output:
Entered boolean array: [True, False] Output Boolean: [False True]
numpy.right_shift()
Right shift function is used for shifting the bits of an integer to the right. For example:
import numpy as np input_array = [15,23,56] shift_right = [1,4,2] print ("Entered number : ", input_array) print ("Number of right shifts : ", shift_right ) output_array = np.right_shift(input_array, shift_right) print ("After right shifting : ", output_array)
Output:
Entered number : [15, 23, 56] Number of right shifts : [1, 4, 2] After right shifting : [ 7 1 14]
numpy.left_shift()
Left shift function is used for shifting the bits of an integer to the left. For example:
import numpy as np input_array = [15,23,56] shift_left = [1,4,2] print ("Entered number : ", input_array) print ("Number of left shifts : ", shift_left ) output_array = np.left_shift(input_array, shift_left) print ("After left shifting : ", output_array)
Output:
Entered number : [15, 23, 56] Number of left shifts : [1, 4, 2] After left shifting : [ 30 368 224]