使用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))輸出
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]
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP