將整數的位向左移動,並將移動次數設定為 NumPy 中的陣列
要將整數的位向左移動,請在 Python NumPy 中使用 **numpy.left_shift()** 方法。我們已將移動次數設定為一個新陣列。
透過在 x1 的右側追加 x2 個 0 來將位向左移動。由於數字的內部表示形式為二進位制格式,因此此操作等效於將 x1 乘以 2**x2。x1 是輸入值。x2 是要追加到 x1 的零的數量。必須是非負數。如果 x1.shape != x2.shape,則它們必須可廣播到一個公共形狀(這將成為輸出的形狀)。
函式 left_shift() 返回 x1,其位向左移動 x2 次。如果 x1 和 x2 都是標量,則這是一個標量。
步驟
首先,匯入所需的庫 -
import numpy as np
建立一個一維陣列 -
arrLeft = np.array([2, 3, 5])
顯示我們的陣列 -
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.left_shift() 方法。我們已將移動次數設定為陣列 arrLeft -
print("
Result (left shift)...
",np.left_shift(val, arrLeft))示例
import numpy as np
# Create a One-Dimensional array
arrLeft = np.array([2, 3, 5])
# Displaying our array
print("Array...
",arrLeft)
# Get the datatype
print("
Array datatype...
",arrLeft.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arrLeft.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arrLeft.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arrLeft.size)
# The actual integer value
val = 25
# To shift the bits of an integer to the left, use the numpy.left_shift() method in Python Numpy
# We have set the count of shifts as an array arrLeft
print("
Result (left shift)...
",np.left_shift(val, arrLeft))輸出
Array... [2 3 5] Array datatype... int64 Array Dimensions... 1 Our Array Shape... (3,) Elements in the Array... 3 Result (left shift)... [100 200 800]
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP