
- NumPy 教程
- NumPy - 首頁
- NumPy - 簡介
- NumPy - 環境
- NumPy 陣列
- NumPy - Ndarray 物件
- NumPy - 資料型別
- NumPy 建立和運算元組
- NumPy - 陣列建立例程
- NumPy - 陣列操作
- NumPy - 從現有資料建立陣列
- NumPy - 從數值範圍建立陣列
- NumPy - 迭代陣列
- NumPy - 陣列重塑
- NumPy - 連線陣列
- NumPy - 堆疊陣列
- NumPy - 分割陣列
- NumPy - 展平陣列
- NumPy - 轉置陣列
- NumPy 索引和切片
- NumPy - 索引和切片
- NumPy - 高階索引
- NumPy 陣列屬性和操作
- NumPy - 陣列屬性
- NumPy - 陣列形狀
- NumPy - 陣列大小
- NumPy - 陣列步長
- NumPy - 陣列元素大小
- NumPy - 廣播
- NumPy - 算術運算
- NumPy - 陣列加法
- NumPy - 陣列減法
- NumPy - 陣列乘法
- NumPy - 陣列除法
- NumPy 高階陣列操作
- NumPy - 交換陣列軸
- NumPy - 位元組交換
- NumPy - 複製和檢視
- NumPy - 元素級陣列比較
- NumPy - 過濾陣列
- NumPy - 連線陣列
- NumPy - 排序、搜尋和計數函式
- NumPy - 搜尋陣列
- NumPy - 陣列的並集
- NumPy - 查詢唯一行
- NumPy - 建立日期時間陣列
- NumPy - 二元運算子
- NumPy - 字串函式
- NumPy - 數學函式
- NumPy - 統計函式
- NumPy - 矩陣庫
- NumPy - 線性代數
- NumPy - Matplotlib
- NumPy - 使用 Matplotlib 繪製直方圖
- NumPy - NumPy 的 I/O
- NumPy 排序和高階操作
- NumPy - 陣列排序
- NumPy - 沿軸排序
- NumPy - 使用花式索引排序
- NumPy - 結構化陣列
- NumPy - 建立結構化陣列
- NumPy - 操作結構化陣列
- NumPy - 欄位訪問
- NumPy - 記錄陣列
- Numpy - 載入陣列
- Numpy - 儲存陣列
- NumPy - 將值追加到陣列
- NumPy - 交換陣列的列
- NumPy - 將軸插入陣列
- NumPy 處理缺失資料
- NumPy - 處理缺失資料
- NumPy - 識別缺失值
- NumPy - 刪除缺失資料
- NumPy - 估算缺失資料
- NumPy 效能最佳化
- NumPy - 使用陣列進行效能最佳化
- NumPy - 使用陣列進行向量化
- NumPy - 陣列的記憶體佈局
- Numpy 線性代數
- NumPy - 線性代數
- NumPy - 矩陣庫
- NumPy - 矩陣加法
- NumPy - 矩陣減法
- NumPy - 矩陣乘法
- NumPy - 元素級矩陣運算
- NumPy - 點積
- NumPy - 矩陣求逆
- NumPy - 行列式計算
- NumPy - 特徵值
- NumPy - 特徵向量
- NumPy - 奇異值分解
- NumPy - 求解線性方程組
- NumPy - 矩陣範數
- NumPy 元素級矩陣運算
- NumPy - 求和
- NumPy - 求平均值
- NumPy - 求中位數
- NumPy - 求最小值
- NumPy - 求最大值
- NumPy 集合運算
- NumPy - 唯一元素
- NumPy - 交集
- NumPy - 並集
- NumPy - 差集
- NumPy 有用資源
- NumPy 編譯器
- NumPy - 快速指南
- NumPy - 有用資源
- NumPy - 討論
NumPy 右移() 函式
NumPy 的right_shift()函式用於對陣列的元素執行按位右移運算。此函式將輸入陣列中每個元素的位向右移動指定數量的位置。每當一位移動時,在左側插入一個零。
此函式可以接受陣列或標量作為輸入,以及要移動的位置數。結果是一個與輸入形狀相同的陣列,其中包含移位後的值。它通常用於涉及按位操作的任務,例如二進位制算術或數字訊號處理。
語法
以下是 Numpy right_shift() 函式的語法:
numpy.right_shift(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True, [,signature])
引數
Numpy right_shift() 函式接受以下引數:
- x1: 應用右移運算的輸入值。
- x2: 要應用的右移位置數。
- out(ndarray, None 或 ndarray 和 None 的元組,可選):儲存結果的位置。
- where(array_like,可選):這是必須應用右移運算的條件。
- **kwargs: 引數(如 casting、order、dtype、subok)是其他關鍵字引數,可根據需要使用。
返回值
此函式返回一個數組,其中每個元素都是將輸入值中相應元素的位向右移動 shift 指定的位置數的結果。
示例 1
以下是 Numpy right_shift() 函式的基本示例,其中將整數的位向右移動指定數量的位置,有效地將數字除以 2 的冪:
import numpy as np # Define an integer number = 16 # Binary: 00010000 # Define the number of positions to shift shift_positions = 2 # Perform the right shift operation result = np.right_shift(number, shift_positions) print('Original number:', number) print('Binary representation:', np.binary_repr(number, width=8)) print(f'Right shift by {shift_positions} positions results in:', result) print('Binary representation after shift:', np.binary_repr(result, width=8))
以下是應用於整數的right_shift() 函式的輸出:
Original number: 16 Binary representation: 00010000 Right shift by 2 positions results in: 4 Binary representation after shift: 00000100
示例 2
在處理按位移位運算時,通常不支援負移位值。嘗試使用負移位值通常會導致錯誤或未定義的行為,具體取決於實現和特定的操作。以下是它的示例:
import numpy as np # Define an array of integers array = np.array([16, 32, 64, 128]) # Define a negative number of positions to shift negative_shift_positions = -2 try: # Attempt to perform the right shift operation with a negative shift value result = np.right_shift(array, negative_shift_positions) print('Result with negative shift value:', result) except ValueError as e: print('Error:', e)
以下是負右移的輸出:
Result with negative shift value: [0 0 0 0]
示例 3
這是使用right_shift() 函式執行位右移運算的另一個示例:
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 result = np.right_shift(arr, valRight) # Display the result of the right shift operation print("Result (right shift):", result)
以下是右移的輸出:
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]
numpy_binary_operators.htm
廣告