在 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
创建一个 0d 数组 -
arr = np.array(65)
显示我们的数组 -
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 = 2
要将整数的位右移,请在 Python NumPy 中使用 numpy.right_shift() 方法 -
print("
Result (right shift)...
",np.right_shift(arr, valRight))示例
import numpy as np
# Create a 0d array
arr = np.array(65)
# 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 = 2
# 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... 65 Array datatype... int64 Array Dimensions... 0 Our Array Shape... () Elements in the Array... 1 Result (right shift)... 16
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP