NumPy 中整数的左移位


要将整数的位向左移动,请在 Python NumPy 中使用 **numpy.left_shift()** 方法。位向左移动是在 x1 的右侧追加 x2 个 0。由于数字的内部表示形式为二进制格式,因此此操作等效于将 x1 乘以 2**x2。x1 是输入值。x2 是要追加到 x1 的零的个数。必须是非负数。如果 x1.shape != x2.shape,则它们必须可广播到公共形状(这将成为输出的形状)。

left_shift() 函数返回将位向左移动 x2 次的 x1。如果 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)

左移的次数:

valLeft = 2

要将整数的位向左移动,请在 Python NumPy 中使用 numpy.left_shift() 方法:

print("
Result (left shift)...
",np.left_shift(arr, valLeft))

示例

Open Compiler
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 left shift valLeft = 2 # To shift the bits of an integer to the left, use thennumpy.left_shift() method in Python Numpy print("Result (left shift)...",np.left_shift(arr, valLeft))

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

Array...
65

Array datatype...
int64

Array Dimensions...
0

Our Array Shape...
()

Elements in the Array...
1

Result (left shift)...
260

更新于:2022年2月17日

141 次查看

启动您的职业生涯

通过完成课程获得认证

开始
广告