使用NumPy将整数的位向右移动,并将移动计数设置为一个NumPy的带符号整型数组。
要将整数的位向右移动,请使用Python NumPy中的**numpy.right_shift()**方法。我们将移动计数设置为一个新数组。位向右移动x2位。因为数字的内部表示是二进制格式,所以此操作等效于将x1除以2**x2。
x1是输入值。x2是要从x1右侧移除的位数。如果x1.shape != x2.shape,则它们必须可广播到公共形状。
right_shift()函数返回x1,其位向右移动x2位。如果x1和x2都是标量,则这是一个标量。
步骤
首先,导入所需的库:
import numpy as np
创建一个一维数组。数据类型使用“dtype”参数设置。我们将数据类型设置为带符号整型:
arrRight = np.array([2, 3, 5],dtype=np.int8)
显示我们的数组:
print("Array...",arr)
获取数据类型:
print("Array datatype...",arr.dtype)
获取数组的维度:
print("Array Dimensions...",arr.ndim)
获取数组的形状:
print("Our Array Shape...",arr.shape)
获取数组的元素个数:
print("Elements in the Array...",arr.size)
实际整数值:
val = 25
要将整数的位向右移动,请使用numpy.right_shift()方法。我们将移动计数设置为数组arrRight:
print("Result (right shift)...",np.right_shift(val, arrRight))
示例
import numpy as np # Create a One-Dimensional array # The datatype is set using the "dtype" parameter # We have set the datatype to signed integer type arrRight = np.array([2, 3, 5],dtype=np.int8) # Displaying our array print("Array...",arrRight) # Get the datatype print("Array datatype...",arrRight.dtype) # Get the dimensions of the Array print("Array Dimensions...",arrRight.ndim) # Get the shape of the Array print("Our Array Shape...",arrRight.shape) # Get the number of elements of the Array print("Elements in the Array...",arrRight.size) # The actual integer value val = 25 # To shift the bits of an integer to the right, use the numpy.right_shift() method in Python Numpy # We have set the count of shifts as an array arrRight print("Result (right shift)...",np.right_shift(val, arrRight))
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
Array... [2 3 5] Array datatype... int8 Array Dimensions... 1 Our Array Shape... (3,) Elements in the Array... 3 Result (right shift)... [6 3 0]
广告