在 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
创建一个一维数组 -
arr = np.array([56, 87, 23, 92, 81, 98, 45, 98])
显示我们的数组 -
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)右移位的次数 -
valRight = 3
要将整数数组元素的位向右移位,请在 Python Numpy 中使用 numpy.right_shift() 方法 -
print("
Result (right shift)...
",np.right_shift(arr, valRight))示例
import numpy as np
# Create a One-Dimensional array
arr = np.array([56, 87, 23, 92, 81, 98, 45, 98])
# Displaying our array
print("Array...
",arr)
# Get the datatype
print("
Array datatype...
",arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arr.size)
# The count of right shift
valRight = 3
# To shift the bits of an integer to the right, use the numpy.right_shift() method in Python Numpy
print("
Result (right shift)...
",np.right_shift(arr, valRight))输出
Array... [56 87 23 92 81 98 45 98] Array datatype... int64 Array Dimensions... 1 Our Array Shape... (8,) Elements in the Array... 8 Result (right shift)... [ 7 10 2 11 10 12 5 12]
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP