Overview:
-
The numpy.left_shift() function shifts the bits of the integer or Boolean elements from the first array-like instance by the corresponding element from second array times to the left, replacing the same number of rightmost bits with zero.
-
The effect of above left shift operation is equivalent to multiplying the left integer by two to the power the right integer times.
-
The bitwise left shift operation has its applications in faster multiplication of numbers, in image processing while encoding/decoding of images and in cryptography.
Example:
# Example Python program that uses the numpy.left_shift() # To convert the ndarrays to binary form # Create ndarrays a1[0][0] = 10 a2[0][0] = 1 # Do left shift of (a1, a2) print("Array2:") print("Array1 in binary:") print("Array2 in binary:") print("Bit pattern of array1 << array2 :") print("Array1 in decimal after applying numpy.left_shift(array1, array2):") |
Output:
Array1: [[10 20 30]] Array2: [[1 2 3]] Array1 in binary: [['00001010' '00010100' '00011110']] Array2 in binary: [['00000001' '00000010' '00000011']] Bit pattern of array1 << array2 : [['00010100' '01010000' '11110000']] Array1 in decimal after applying numpy.left_shift(array1, array2): [[ 20 80 240]] |