NumPy Shift Functions

In this article, we will discuss about NumPy Shift Functions that are numpy.left_shift() and numpy.right_shift() with some code snippets.

Introduction to NumPy Shift Functions

NumPy left_shift

To move the bits of an integer to the left, use the numpy.left_shift() function. By adding arr2 0s(zeroes) to the right of arr1, the bits are moved to the left. This operation is equal to multiplying arr1 by 2*arr2 since the underlying representation of integers is in binary format. For example, if the integer is 3 and we wish to left shift 3 bits, the outcome will be 3(2^2) = 12 after left shift 2 bits.

Syntax:

numpy.left_shift(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, ufunc ‘left_shift’)

Parameters:

  • arr1: [array_like] of integer type
  • arr2: [array_like] of integer type
    • Number of zeros to append to arr1.The value of arr2 should be a positive integer.
  • out: [ndarray, optional] A location in which the result is stored.
    • If provided, it must have a shape that the inputs broadcast to.
    • If not provided or None, a freshly-allocated array is returned.
  • **kwargs: allows you to pass keyword variable length of argument to a function. It is used when we want to handle a named argument in a function.
  • where: [array_like, optional]True value means to calculate the universal functions(ufunc) at that position, False value means to leave the value in the output alone.

Return: array of integer type. Return arr1 with bits shifted arr2 times to the left. This is a scalar if both arr1 and arr2 are scalars.

NumPy right_shift

To move the bits of an integer to the right, use the numpy.right_shift() function. This operation is equal to dividing arr1 by 2**arr2 since the underlying representation of integers is in binary format. For example, if the integer is 16 and we wish to right shift it by two bits, the output will be 16/(2^2) = 4.

Syntax:

numpy.right_shift(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, ufunc ‘right_shift’)

Parameters:

  • arr1: [array_like] of integer type
  • arr2: [array_like] of integer type
    • The number of bits we have to remove at the right of arr1.
  • out: [ndarray, optional] A location in which the result is stored.
    • If provided, it must have a shape that the inputs broadcast to.
    • If not provided or None, a freshly-allocated array is returned.
  • **kwargs: allows you to pass keyword variable length of argument to a function. It is used when we want to handle a named argument in a function.
  • where: [array_like, optional] True value means to calculate the universal functions(ufunc) at that position, False value means to leave the value in the output alone.

Return: an array of integer types. Return arr1 with bits shifted arr2 times to the right. This is a scalar if both arr1 and arr2 are scalars.

Code Examples of NumPy Shift Functions

NumPy left_shift

Example 01: When inputs and bit shift are numbers

# welcome to softhunt.net
# Python program explaining
# left_shift() function

import numpy as np
num = 3
bit_shift_num = 2

print ("Input number : ", num)
print ("Number of bit shift : ", bit_shift_num )
	
ans = np.left_shift(num, bit_shift_num)
print ("After left shifting 2 bit : ", ans)

Output:

Input number :  3
Number of bit shift :  2
After left shifting 2 bit :  12

Example 02: When inputs and bit shift are an arrays

# welcome to softhunt.net
# Python program explaining
# left_shift() function

import numpy as np

arr = [21, 15, 43]
bit_shift_arr =[2, 3, 4]

print ("Input array : ", arr)
print ("array of bit shift : ", bit_shift_arr )
	
ans = np.left_shift(arr, bit_shift_arr)
print ("After left shifting 2 bit : ", ans)

Output:

Input array :  [21, 15, 43]
array of bit shift :  [2, 3, 4]
After left shifting 2 bit :  [ 84 120 688]

NumPy right_shift

Example 01: When inputs and bit shift are numbers

# welcome to softhunt.net
# Python program explaining
# right_shift() function

import numpy as np
num = 16
bit_shift_num = 2

print ("Input number : ", num)
print ("Number of bit shift : ", bit_shift_num )
	
ans = np.right_shift(num, bit_shift_num)
print ("After right shifting 2 bit : ", ans)

Output:

Input number :  16
Number of bit shift :  2
After right shifting 2 bit :  4

Example 02: When inputs and bit shift are an arrays

# welcome to softhunt.net
# Python program explaining
# right_shift() function

import numpy as np

arr = [21, 15, 43]
bit_shift_arr =[2, 3, 4]

print ("Input array : ", arr)
print ("array of bit shift : ", bit_shift_arr )
	
ans = np.right_shift(arr, bit_shift_arr)
print ("After right shifting 2 bit : ", ans)

Output:

Input array :  [21, 15, 43]
array of bit shift :  [2, 3, 4]
After right shifting 2 bit :  [5 1 2]

FAQs

What is Python NumPy package?

NumPy is the most important Python module for scientific computing. It’s a Python library that includes a multidimensional array object, derived objects (such as masked arrays and matrices), and a variety of routines for performing fast array operations, such as mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation, and more.

NumPy Shift Functions: Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.invert() – The NumPy invert Python Function

Leave a Comment