📜  Python中的 numpy.left_shift()

📅  最后修改于: 2022-05-13 01:54:28.065000             🧑  作者: Mango

Python中的 numpy.left_shift()

numpy.left_shift()函数用于将整数的位向左移动。

通过在 arr1 的右侧附加 arr2 0s(零),这些位向左移动。由于数字的内部表示是二进制格式,所以这个操作相当于 arr1 乘以 2**arr2。例如,如果数字是 5,我们想要左移 2 位,那么在左移 2 位之后,结果将是 5*(2^2) = 20

代码#1:工作

# Python program explaining
# left_shift() function
  
import numpy as geek
in_num = 5
bit_shift = 2
  
print ("Input  number : ", in_num)
print ("Number of bit shift : ", bit_shift ) 
    
out_num = geek.left_shift(in_num, bit_shift) 
print ("After left shifting 2 bit  : ", out_num) 

输出 :

Input  number :  5
Number of bit shift :  2
After left shifting 2 bit  :  20


代码#2:

# Python program explaining
# left_shift() function
  
import numpy as geek
  
in_arr = [2, 8, 15]
bit_shift =[3, 4, 5]
   
print ("Input array : ", in_arr) 
print ("Number of bit shift : ", bit_shift)
    
out_arr = geek.left_shift(in_arr, bit_shift) 
print ("Output array after left shifting: ", out_arr) 

输出 :

Input array :  [2, 8, 15]
Number of bit shift :  [3, 4, 5]
Output array after left shifting:  [ 16 128 480]